Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用R中的其他值替换数据框中的值_R_Replace_Dataframe - Fatal编程技术网

用R中的其他值替换数据框中的值

用R中的其他值替换数据框中的值,r,replace,dataframe,R,Replace,Dataframe,我有两个数据框tab_A和tab_b,具有相同的列名:group,V1:V3。第一个具有附加列id。选项卡A中的每一行的值都在V1:V3中 tab_A <- data.frame(cbind(id = rep(c("01", "02", "03"),3), gr = c("A","A","A","B","B","B","C","C","C"), V1 = c(NA,NA,"A","B",NA,NA,

我有两个数据框
tab_A
tab_b
,具有相同的列名:
group
V1:V3
。第一个具有附加列
id
选项卡A
中的每一行的值都在
V1:V3

tab_A <- data.frame(cbind(id = rep(c("01", "02", "03"),3),
                       gr = c("A","A","A","B","B","B","C","C","C"),
                       V1 = c(NA,NA,"A","B",NA,NA,NA,"C","C"),
                       V2 = c("A",NA,NA,NA,"B","B","C",NA,NA),
                       V3 = c(NA,"A",NA,"B",NA,"B",NA,"C",NA)))
tab_b <- data.frame(cbind(gr = c("A","B","C"), V1 = c(5,2,9), V2 = c(0,1,5), 
                       V3 = c(4,4,3)))

提前感谢您的关注。

下面是一个使用子集创建新数据帧的解决方案:

tab_A <- data.frame(cbind(id = rep(c("01", "02", "03"),3),
                       gr = c("A","A","A","B","B","B","C","C","C"),
                       V1 = c(NA,NA,"A","B",NA,NA,NA,"C","C"),
                       V2 = c("A",NA,NA,NA,"B","B","C",NA,NA),
                       V3 = c(NA,"A",NA,"B",NA,"B",NA,"C",NA)))
tab_b <- data.frame(cbind(gr = c("A","B","C"), V1 = c(5,2,9), V2 = c(0,1,5), 
                       V3 = c(4,4,3)))

df <- data.frame(id = tab_A$id,
                 gr = tab_A$gr, 
                 V1 = tab_b$V1[tab_A$V1], 
                 V2 = tab_b$V2[tab_A$V2], 
                 V3 = tab_b$V3[tab_A$V3])

df

      ###
  id gr   V1   V2   V3
1 01  A <NA>    0 <NA>
2 02  A <NA> <NA>    4
3 03  A    5 <NA> <NA>
4 01  B    2 <NA>    4
5 02  B <NA>    1 <NA>
6 03  B <NA>    1    4
7 01  C <NA>    5 <NA>
8 02  C    9 <NA>    3
9 03  C    9 <NA> <NA>

tab_A因为
tab_A
tab_b
的维度不匹配,我不确定如何用后者中的值替换前者中的值。你能澄清一下吗?对不起,我觉得尺寸没有问题。在
选项卡A$gr
中,我有三个唯一的值:A、B、C。我想用
选项卡B
中的值替换其他列中的值。i、 e.
subset(tab_b,gr==“A”,select=“V1”)
=5应该放在
subset(tab_A[3],gr==“A”,select=“V1”)
等每个值上,除了
谢谢,但为什么可以帮我处理2000多列?我可以转义一下定义每一列吗?是的,这是可能的,但问题不同:)你能举一个你正在处理的多列框架的例子吗?我不知道如何在循环中实现你的@MarkeD子集。下面是示例数据,sample.csv=tab_A,dat.csv=tab_b
tab_A <- data.frame(cbind(id = rep(c("01", "02", "03"),3),
                       gr = c("A","A","A","B","B","B","C","C","C"),
                       V1 = c(NA,NA,"A","B",NA,NA,NA,"C","C"),
                       V2 = c("A",NA,NA,NA,"B","B","C",NA,NA),
                       V3 = c(NA,"A",NA,"B",NA,"B",NA,"C",NA)))
tab_b <- data.frame(cbind(gr = c("A","B","C"), V1 = c(5,2,9), V2 = c(0,1,5), 
                       V3 = c(4,4,3)))

df <- data.frame(id = tab_A$id,
                 gr = tab_A$gr, 
                 V1 = tab_b$V1[tab_A$V1], 
                 V2 = tab_b$V2[tab_A$V2], 
                 V3 = tab_b$V3[tab_A$V3])

df

      ###
  id gr   V1   V2   V3
1 01  A <NA>    0 <NA>
2 02  A <NA> <NA>    4
3 03  A    5 <NA> <NA>
4 01  B    2 <NA>    4
5 02  B <NA>    1 <NA>
6 03  B <NA>    1    4
7 01  C <NA>    5 <NA>
8 02  C    9 <NA>    3
9 03  C    9 <NA> <NA>