简体   繁体   中英

Use a column in a data.frame to select another

How do I use one variable in a data.frame to refer to another?

say I have:

col     col1     col2
"col1"    1        5
"col2"    2        6
"col1"    3        7
"col2"    4        8

and I want:

col     col1     col2     answer
"col1"    1        5         1
"col2"    2        6         6
"col1"    3        7         3
"col2"    4        8         8

,

df$answer = df[,df$col]

isn't working, and a for loop is taking forever.

This doesn't look very hard, but the solution I found isn't very elegant, there are probably better ways. But you can use match and then subset according to the match:

dat <- read.table(text="col     col1     col2
col1    1        5
col2    2        6
col1    3        7
col2    4        8", header = T, stringsAsFactors = FALSE)

cols <- unique(dat$col)
matches <- match(dat$col, cols)

dat$answer <- sapply(seq_along(matches), function (i) {
  dat[i,cols[matches[i]]]
})

And the result:

> dat
   col col1 col2 answer
1 col1    1    5      1
2 col2    2    6      6
3 col1    3    7      3
4 col2    4    8      8

Edit

Actually, here's an already much better approach:

dat$answer <- sapply(1:nrow(dat), function(r) {
  dat[r,dat$col[r]]
})

This is apparently what you have tried, but using sapply instead of unlist(lapply , so yeah, not sure if this helps.

I know it's already answered, but I thought another approach might be useful:

read.table(text='col     col1     col2
 "col1"    1        5
 "col2"    2        6
 "col1"    3        7
 "col2"    4        8',h=T)->df

df$answer <- as.integer(df[ cbind(c(1:nrow(df)), match(df$col, names(df))) ])
df
#    col col1 col2 answer
# 1 col1    1    5      1
# 2 col2    2    6      6
# 3 col1    3    7      3
# 4 col2    4    8      8

In this case with only 2 columns ifelse might be the fastest and most straightforward solution.

df$answer <- ifelse(df[,1] == "col1",df[,"col1"],df[,"col2”])

       col col1 col2 answer
1 col1    1    5      1
2 col2    2    6      6
3 col1    3    7      3
4 col2    4    8      8

Addition as N8TRO asked in his comment for a more general solution. A simple switch might be all that is needed:

for(i in 1:nrow(df)) df$ans[i] <- switch(df[i,1],df[i,df[i,1]])

or without a "for" loop:

df$ans <- sapply(1:nrow(df),function(i) switch(df[i,1],df[i,df[i,1]]))

example:

df <- data.frame(col=sample(paste0('col',1:5),10,replace=T),col1=1:10,col2=11:20,col3=21:30,col4=31:40,col5=41:50,stringsAsFactors = F)

select the elements:

df$ans <- sapply(1:nrow(df),function(i) switch(df[i,1],df[i,df[i,1]]))

df
    col col1 col2 col3 col4 col5 ans
1  col1    1   11   21   31   41   1
2  col1    2   12   22   32   42   2
3  col5    3   13   23   33   43  43
4  col2    4   14   24   34   44  14
5  col3    5   15   25   35   45  25
6  col4    6   16   26   36   46  36
7  col5    7   17   27   37   47  47
8  col3    8   18   28   38   48  28
9  col1    9   19   29   39   49   9
10 col5   10   20   30   40   50  50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM