使用第三列中的值填充我的数据框中以其他列为条件的列

问题描述 投票:3回答:3

警告:这个问题看起来很容易,我作为一个初学者可能无法在SO上更复杂的主题中找到正确的解决方案(看上去hereherehere以及更多地方)

我想在我的数据框中填写一列,基于另一列,并使用其他列作为输入。这个例子更加清晰:

  Version1 Version2 Version3 Version4 Presented_version Color
1     blue      red    green   yellow                 1    NA
2      red     blue   yellow    green                 4    NA
3   yellow    green      red     blue                 3    NA

我想用版本1 /版本2 /版本3 /版本4的值填充“颜色”列。列的Presented_version告诉我需要这四个值中的哪一个。例如,在第1行中,Presented_version为1,因此所需的值为“Version1”(“blue”)。第1行中的颜色应为蓝色。

有人可以告诉我一种方法来做到这一点,而无需使用大量的“if”语句循环数据框架吗?

structure(list(Version1 = structure(1:3, .Label = c("blue", "red", 
"yellow"), class = "factor"), Version2 = structure(c(3L, 1L, 
2L), .Label = c("blue", "green", "red"), class = "factor"), Version3 = structure(c(1L, 
3L, 2L), .Label = c("green", "red", "yellow"), class = "factor"), 
    Version4 = structure(3:1, .Label = c("blue", "green", "yellow"
    ), class = "factor"), Presented_version = c(1L, 4L, 3L), 
    Color = c(NA, NA, NA)), class = "data.frame", row.names = c(NA, 
-3L))

=======================编辑!

我简化了示例来解释我的问题,但上面的示例在几个方面与我的实际数据集有所不同,因此解决方案做出了我的数据实际上不符合的假设。这是data.frame的更准确的表示。特别是,Presented_version与Version1 ... Version 4列的内容之间没有固定的匹配(根据额外的列而不同,我现在称之为Painter),而Version1到Version4不一定在第1列到第4列中在我的数据集中。

  FillerColumn Painter Version1 Version2 Version3 Version4 Version_presented Color FillerColumn.1
1           77       A     blue      red    green   yellow                 1    NA             77
2           77       B      red     blue   yellow    green                 4    NA             77
3           77       C   yellow    green      red     blue                 3    NA             77
4           77       D      red     blue   yellow    green                 1    NA             77
structure(list(FillerColumn = c(77L, 77L, 77L, 77L), Painter = structure(1:4, .Label = c("A", 
"B", "C", "D"), class = "factor"), Version1 = structure(c(1L, 
2L, 3L, 2L), .Label = c("blue", "red", "yellow"), class = "factor"), 
    Version2 = structure(c(3L, 1L, 2L, 1L), .Label = c("blue", 
    "green", "red"), class = "factor"), Version3 = structure(c(1L, 
    3L, 2L, 3L), .Label = c("green", "red", "yellow"), class = "factor"), 
    Version4 = structure(c(3L, 2L, 1L, 2L), .Label = c("blue", 
    "green", "yellow"), class = "factor"), Version_presented = c(1L, 
    4L, 3L, 1L), Color = c(NA, NA, NA, NA), FillerColumn.1 = c(77L, 
    77L, 77L, 77L)), class = "data.frame", row.names = c(NA, 
-4L))
r dataframe conditional multiple-columns
3个回答
1
投票

使用mapply的一种方法

cols <- grep("^Version", names(df))
df$Color <- unlist(mapply(function(x, y) df[x, cols][y], 
                   1:nrow(df),df$Presented_version))

df
#  Version1 Version2 Version3 Version4 Presented_version Color
#1     blue      red    green   yellow                 1  blue
#2      red     blue   yellow    green                 4 green
#3   yellow    green      red     blue                 3   red

apply

apply(df, 1, function(x) x[cols][as.numeric(x["Presented_version"])])
#[1] "blue"  "green" "red" 

4
投票

我们可以使用带有row/column索引的矢量化选项来提取值而不是任何循环

df1$color <- df1[1:4][cbind(1:nrow(df1), df1$Presented_version)]
df1$color
#[1] "blue"  "green" "red"  

Benchmarks

dfN <- df1[rep(seq_len(nrow(df1)), 1e6),]


system.time({
   dfN[1:4][cbind(1:nrow(dfN), dfN$Presented_version)]

 })
# user  system elapsed 
#   1.216   0.110   1.321


system.time({
 cols <- grep("^Version", names(dfN))
 unlist(mapply(function(x, y) dfN[x, cols][y], 
                    1:nrow(dfN),dfN$Presented_version))

 })
#  user  system elapsed 
# 319.907   1.644 322.418 

现在,让我们看看apply的另一个选项

system.time({
  apply(dfN, 1, function(x) x[cols][as.numeric(x["Presented_version"])])
 }) 
#  user  system elapsed 
# 14.240   0.365  14.550 

2
投票

我喜欢弄乱数据集。尝试data.table melt方法

df <- setDT(df)

df1 <- melt.data.table(df,
                       id.vars = c('Presented_version'),
                       measure.vars = patterns('Version'),
                       value.name = 'Color',
                       variable.name = 'Version')[
  , version1 := str_extract(Version, '\\d+')][
    Presented_version == version1][
      version1 := NULL]

导致

   Presented_version  Version Color 
1:                 1 Version1  blue        
2:                 3 Version3   red        
3:                 4 Version4 green      

而且,如果您希望信息采用相同的原始结构

merge(df, 
      df1[, .(Presented_version, Color)],
      by = 'Presented_version')

   Presented_version Version1 Version2 Version3 Version4 Color
1:                 1     blue      red    green   yellow  blue
2:                 3   yellow    green      red     blue   red
3:                 4      red     blue   yellow    green green  
© www.soinside.com 2019 - 2024. All rights reserved.