在特定位置组合两个不同长度的数据帧

时间:2016-09-19 10:46:26

标签: r dataframe merge

我有两个数据框,一个有七行,另一个有两行。以下是两个框架:

                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

2 个答案:

答案 0 :(得分:2)

例如

row.names(df2) <- c(1,5)
merge(df, df2, by="row.names", all.x=TRUE)[,-1]
#                 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>

答案 1 :(得分:1)

我认为您可以通过创建空列然后有条件地填充它们来轻松完成

df3<- df
df3
df3$rating<- NA
df3$text_type<- NA

df3$rating[df3$ChatPosition=="START"]<- df2$rating
df3$text_type[df3$ChatPosition=="START"]<- as.character(df2$text_type)

df3

修改:在此我假设你想在标有START的行中插入评级