Pulling elements from one list to another under a R criterion

Asked

Viewed 49 times

4

The vector below indicates the regressions I circled.

regression_pairs=c("A~B", "C~D", "E~F", "G~H","I~J","K~L","M~N","O~P","Q~R")

regression_pairs

Below is the list where I saved the residuals of each regression residuals.new:

    residuals.new <- list()
     a=c(1,2,3,4,5)
 b=c(6,7,8,9,10)
 c=c(11,12,13,14,15)
 d=c(16, 17, 18, 19, 20)
 e=c(21,22,23,24,25)
 f=c(26,27,28,29,30)
 g=c(31,32,33,34,35)
 h=c(36,37,38,39,40)
 i=c(41,42,43,44,45)

 residuals.new[[1]]=a
 residuals.new[[2]]=b
 residuals.new[[3]]=c
 residuals.new[[4]]=d
 residuals.new[[5]]=e
residuals.new[[6]]=f
residuals.new[[7]]=g
residuals.new[[8]]=h
residuals.new[[9]]=i

residuals.new

That is, the residues of regression A~B sane 1,2,3,4,5 , C~D regression residues are 6,7,8,9,10 and so on.

After a certain criterio saw that the best pairs are those represented by the list below:

bestresults<-list()

best_pairs=c("A~B", "G~H", "Q~R")

Now I need to figure out a way to create a list that takes the residuals referring to the regressions presented by the list best_pairs.

Someone could help me?

Below is the result I seek:

the.bestresiduals<-list()

 the.bestresiduals[[1]]=c(1,2,3,4,5)
 the.bestresiduals[[2]]=c(16, 17, 18, 19, 20)
 the.bestresiduals[[3]]=c(41,42,43,44,45)

the.bestresiduals

    [[1]]
    [1] 1 2 3 4 5

    [[2]]
    [1] 16 17 18 19 20

    [[3]]
    [1] 41 42 43 44 45

Some help?

2 answers

4

You can use the function match it will return the position of the elements of best_pairs in the vector regression_pairs

regression_pairs=c("A~B", "C~D", "E~F", "G~H","I~J","K~L","M~N","O~P","Q~R")

residuals.new <- list(
                        a=c(1,2,3,4,5),
                        b=c(6,7,8,9,10),
                        c=c(11,12,13,14,15),
                        d=c(16, 17, 18, 19, 20),
                        e=c(21,22,23,24,25),
                        f=c(26,27,28,29,30),
                        g=c(31,32,33,34,35),
                        h=c(36,37,38,39,40),
                        i=c(41,42,43,44,45)
                      )

best_pairs=c("A~B", "G~H", "Q~R")

residuals.new[c(match(best_pairs, regression_pairs))]
#$a
#[1] 1 2 3 4 5

#$d
#[1] 16 17 18 19 20

#$i
#[1] 41 42 43 44 45

2

Use the function %in%. If it’s used as

x %in% y

she crosses x with y. It returns a vector of the size of x, informing the index in y in which are the elements of x. In practice, see what happens in your problem:

regression_pairs %in% best_pairs
TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE

If I save this result in a vector called indices, what I get is

indices <- regression_pairs %in% best_pairs
residuals.new[indices]

[[1]]
[1] 1 2 3 4 5

[[2]]
[1] 16 17 18 19 20

[[3]]
[1] 41 42 43 44 45

More information can be found in the function help: ?match (yes, match is equivalent to %in%, don’t worry).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.