有条件地替换数据框中的值

时间:2019-04-10 17:42:00

标签: r dataframe

我有两个数据框,其中包含七个描述性数据列和可变数量的其他分析列(基于代码中的较早步骤)。我想根据dataframe1第一栏中的布尔值,用dataframe2中相应的值替换dataframe1分析栏中的某些值。

dataframe1

structure(list(compare = c(1, 1, 0, 1, 1, 1, 0, 1), ID_TREE = 29338:29345, 
    ID_PLOT = c(1068L, 1068L, 1068L, 1068L, 1068L, 1068L, 1068L, 
    1068L), ID_CATEGORY = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
    ID_WOOD_SPGR_GREENVOL_DRYWT = c(28L, 28L, 28L, 7L, 28L, 28L, 
    28L, 28L), ID_BARK_SPGR_GREENVOL_DRYWT = c(25L, 25L, 25L, 
    18L, 25L, 25L, 25L, 25L), ID_BARK_VOL_PCT = c(2L, 2L, 2L, 
    10L, 2L, 2L, 2L, 2L), VOLCFGRS = c(3.21875, 6.576453125, 
    12.2406407654729, 0.863593268246, 1.15809306543472, 0.755301358016, 
    13.6662694477056, 4.549483421824)), row.names = c(NA, -8L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: (nil)>)

dataframe2

structure(list(compare = c(1, 1, 0, 1, 1, 1, 0, 1), ID_TREE = 29338:29345, 
    ID_PLOT = c(1068L, 1068L, 1068L, 1068L, 1068L, 1068L, 1068L, 
    1068L), ID_CATEGORY = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
    ID_WOOD_SPGR_GREENVOL_DRYWT = c(28L, 28L, 28L, 7L, 28L, 28L, 
    28L, 28L), ID_BARK_SPGR_GREENVOL_DRYWT = c(25L, 25L, 25L, 
    18L, 25L, 25L, 25L, 25L), ID_BARK_VOL_PCT = c(2L, 2L, 2L, 
    10L, 2L, 2L, 2L, 2L), VOLCFGRS = c(-2.32258333333333, 5.81718680555556, 
    12.2406407654729, -32.9676545519935, -27.9506018960536, -38.5047101237694, 
    13.6662694477056, 1.9138577595677)), row.names = c(NA, -8L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: (nil)>)

到目前为止,我已经获得了以下代码,可用于1列:

df1[df1$compare==0,8]<- df2[df1$compare==0,8]

但是当我尝试将其抽象化以适用于任意数量的列时,我得到一个错误:

df1[df1$compare==0,-(1:7)]<- df2[df1$compare==0,-(1:7)]

我也是这样,并遇到类似的错误:

df1[,-(1:7)]<- ifelse(df1$compare==0, df2[,-(1:7)], df1[,-(1:7)])

两个数据框将始终具有相同的列数。

1 个答案:

答案 0 :(得分:0)

最简单的是,您可以“反转”您的子设置:

df1[df1$compare==0,8:ncol(df1)] <- df2[df1$compare==0,8:ncol(df1)]

另一种选择是将rbind对联的行在一起。

rbind(df1[df1$compare!=0], df2[df1$compare==0])