【发布时间】:2016-09-19 10:46:26
【问题描述】:
我有两个数据框,一个有 7 行,另一个有 2 行。这是两个框架:
content ChatPosition
1 This is a start line START
2 This is a middle line MIDDLE
3 This is a middle line MIDDLE
4 This is the last line END
5 This is a start line START
6 This is a middle line MIDDLE
7 This is the last line END
和
rating text_type
1 0.2324 Postive
2 0.8999 Postive
基本上我想合并两个数据框,但我想合并它们,以便 rating 和 text_type 数据框中的值与第一个数据框的第 1 行和第 5 行中的值对齐。换句话说,来自 df2 的值只能插入到 ChatPosition 值 = "START" 的位置,所以我希望得到一个如下所示的数据框:
content ChatPosition rating text_type
1 This is a start line START 0.2324 Postive
2 This is a middle line MIDDLE NA <NA>
3 This is a middle line MIDDLE NA <NA>
4 This is the last line END NA <NA>
5 This is a start line START 0.8999 Postive
6 This is a middle line MIDDLE NA <NA>
7 This is the last line END NA <NA>
我查看了 stackexchange,似乎有许多与解决类似问题相关的问题和答案,其中 OP 没有为要合并的两个帧指定特定的匹配标准。这里有一些有用的代码,但我无法扩展它来解决我的问题:
combining two data frames of different lengths.
我在下面添加了代码来填充两个数据框。如果有人可以提供帮助,将不胜感激。
content <- c("This is a start line" , "This is a middle line" , "This is a middle line" ,"This is the last line" ,
"This is a start line" , "This is a middle line" , "This is the last line")
ChatPosition <- c("START" , "MIDDLE" , "MIDDLE" , "END" , "START" ,"MIDDLE" , "END")
df <- data.frame(content, ChatPosition)
df
rating <- c(0.2324, 0.8999)
text_type <- c("Postive", "Postive")
df2 <- data.frame(rating, text_type)
df2
【问题讨论】: