简体   繁体   English

使用列作为列索引从 R 中的数据框中提取值

[英]Using a column as a column index to extract value from a data frame in R

I am trying to use the values from a column to extract column numbers in a data frame.我正在尝试使用列中的值来提取数据框中的列号。 My problem is similar to this topic in r-bloggers .我的问题类似于r-bloggers中的这个主题。 Copying the script here:在此处复制脚本:

df <- data.frame(x = c(1, 2, 3, 4),
                 y = c(5, 6, 7, 8),
                 choice = c("x", "y", "x", "z"),
                 stringsAsFactors = FALSE)

However, instead of having column names in choice , I have column index number, such that my data frame looks like this:但是,我没有choice列名,而是列索引号,这样我的数据框看起来像这样:

df <- data.frame(x = c(1, 2, 3, 4),
                 y = c(5, 6, 7, 8),
                 choice = c(1, 2, 1, 3),
                 stringsAsFactors = FALSE)

I tried using this solution:我尝试使用此解决方案:

df$newValue <-
  df[cbind(
    seq_len(nrow(df)),
    match(df$choice, colnames(df))
  )]

Instead of giving me an output that looks like this:而不是给我一个看起来像这样的 output:

#   x y choice newValue
# 1 1 4   1        1
# 2 2 5   2        2
# 3 3 6   1        6
# 4 8 9   3        NA

My newValue column returns all NAs.我的newValue列返回所有 NA。

    # x y choice newValue
    # 1 1 4   1        NA
    # 2 2 5   2        NA
    # 3 3 6   1        NA
    # 4 8 9   3        NA

What should I modify in the code so that it would read my choice column as column index?我应该在代码中修改什么以便它将我的choice列读取为列索引?

As you have column numbers which we need to extract from data frame already we don't need match here.由于您有我们需要从数据框中提取的列号,因此我们不需要在这里match However, since there is a column called choice in the data which you don't want to consider while extracting data we need to turn the values which are not in the range to NA before subsetting from the dataframe.但是,由于数据中有一个称为choice的列,您在提取数据时不想考虑它,我们需要在从 dataframe 进行子集之前将不在范围内的值转换为NA

mat <- cbind(seq_len(nrow(df)), df$choice)
mat[mat[, 2] > (ncol(df) -1), ] <- NA 
df$newValue <- df[mat]

df
#  x y choice newValue
#1 1 5      1        1
#2 2 6      2        6
#3 3 7      1        3
#4 4 8      3       NA

data数据

df <- data.frame(x = c(1, 2, 3, 4),
                 y = c(5, 6, 7, 8),
                 choice = c(1, 2, 1, 3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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