and*_*ndy 16 r match dataframe
我试图填写table1与table2的val2值匹配
table1$New_val2 = table2[table2$pid==table1$pid,]$val2
Run Code Online (Sandbox Code Playgroud)
但是我得到了警告
longer object length is not a multiple of shorter object length
Run Code Online (Sandbox Code Playgroud)
这是公平的,因为表长度不一样.
请以正确的方式指导我这样做.
cor*_*ory 25
merge(table1, table2[, c("pid", "col2")], by="pid")
添加all.x=TRUE参数以保持table1中的所有pid在table2中没有匹配项.
你走在正确的轨道上.这是一种使用匹配的方式......
table1$val2 <- table2$val2[match(table1$pid, table2$pid)]
我不确定你是不是这个意思,但你可能会使用:
newtable <- merge(table1,table2, by = "pid")
Run Code Online (Sandbox Code Playgroud)
这将创建一个名为 newtable 的新表,其中包含 3 列和与 id 匹配的值,在本例中为“pid”。