![](/img/trans.png)
[英]In R how do I remove empty rows from a data frame and extract one value from a cell and paste it in other cell?
[英]How do I select a value based on two conditions from a data frame and then paste it into a cell in another data frame?
我有兩個數據框。 其中一項試驗中出現了兩個物體。 另一個具有有關這兩個對象如何關聯的信息(視覺相似性)。 對於第一個數據幀中的每一行(試用版),我想提取對象的名稱並使用它們來查找第二個數據幀中的特定行。 他們看起來像這樣:
Trial probepic target_pic vissim
1 1 Robot1 Robot6 NA
2 2 Robot1 Robot3 NA
3 3 Robot5 Robot6 NA
4 4 Robot2 Robot1 NA
5 5 Robot3 Robot9 NA
6 6 Robot14 Robot9 NA
Rob1 Rob2 sim
1 Robot1 Robot1 NA
2 Robot2 Robot1 2.88
3 Robot3 Robot1 3.75
4 Robot4 Robot1 1.63
5 Robot5 Robot1 3.63
6 Robot6 Robot1 2.50
我想要的是使用probepic和target_pic變量從第二個數據幀中選擇sim值,並將其粘貼到第一個數據幀中的vissim中。 我一直在研究子集,並且可以使用以下代碼獲得正確的值:
subset(vissim, Rob1=="Robot1" & Rob2=="Robot2")$sim
但是我想為每一行使用probepic
和target_pic
變量,而不是“ Robot1”和“ Robot2”。 最后將功能應用於第一個數據幀中的每一行。 因此,每一行中,該功能會看在probepic
列和值相匹配的Rob1
列,然后看在target_pic
列和匹配值的Rob2
列。 這將在第二個數據幀中指定唯一行。 然后,它將從該唯一行中獲取sim
的值並將其粘貼到vissim
。
我認為您想將數據集合並在一起。 請嘗試以下操作:
newDf <- merge(df1, df2, by.x=c("probepic", "target_pic"), by.y=c("Rob1", "Rob2"), all=T)
數據
df1 <- read.table(header=T, text=" Trial probepic target_pic vissim
1 Robot1 Robot6 NA
2 Robot1 Robot3 NA
3 Robot5 Robot6 NA
4 Robot2 Robot1 NA
5 Robot3 Robot9 NA
6 Robot14 Robot9 NA")
df2 <- read.table(header= T, text=" Rob1 Rob2 sim
Robot1 Robot1 NA
Robot2 Robot1 2.88
Robot3 Robot1 3.75
Robot4 Robot1 1.63
Robot5 Robot1 3.63
Robot6 Robot1 2.50")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.