在特定位置组合两个不同长度的数据帧 [英] combining two data frames of different lengths at specific locations
问题描述
内容ChatPosition
1这是一个起始行START
2这是一个中间线MIDDLE
3这是一个中间线MIDDLE
4这是最后一行END
5这是一个起始行START
6这是一个中间线MIDDLE
7这是最后一行END
和
评分text_type
1 0.2324 Postive
2 0.8999 Postive
基本上我想合并两个数据帧,但是我想要合并它们,以便在rating和text_type数据帧中的值与第一个数据帧的第1行和第5行中的值相符。换句话说,df2的值只能插入到ChatPosition值=START的地方,所以我想得到一个如下所示的数据框:
内容ChatPosition评分text_type
1这是一个起始行START 0.2324 Postive
2这是一个中间线MIDDLE NA< NA>
3这是一个中间线MIDDLE NA< NA>
4这是最后一行END NA< NA>
5这是一个起始行START 0.8999 Postive
6这是一个中间线MIDDLE NA< NA>
7这是最后一行END NA< NA>
我看了一下stackexchange,似乎有一些问题和答案有关解决类似的问题是,OP没有为要合并的两个帧指定特定的匹配条件。这里有一些有用的代码,但是我无法扩展它来解决我的问题:
我已经包含代码以下获取两个数据框的填充。如果有人可以帮助,那将是非常感激的。
content< - c(This is a start line这是一个中间行,这是一个中间行,这是最后一行,
这是一个起始行,这是一个中间行,这是最后一行 )
聊天位置< - c(START,MIDDLE,MIDDLE,END,START,MIDDLE,END)
df< - data.frame(content,ChatPosition)
df
评级< - c(0.2324,0.8999)
text_type< - c(Postive Postive)
df2< - data.frame(rating,text_type)
df2
我想你可以通过创建空列轻轻地完成,然后有条件地填充它们
code> 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
编辑:在这里我假设你想插入评分在$ code> START
的行中
I have two data frames one with seven rows the other with 2 rows. Here are the two frames:
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
and
rating text_type
1 0.2324 Postive
2 0.8999 Postive
Basically I want to merge the two data frames, but I want to merge them so that the values in the rating and text_type data frame line up with values in the 1st and 5th rows of the first data frame. In other words the values from df2 should only be inserted where the ChatPosition value = "START" So i want to end up with a dataframe that looks like this:
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>
I had a look around stackexchange, there seems to be a number of questions and answers related to solving a similar problem where the OP doesn't specify a specific matched criteria for the two frames to be merged. There is some useful code here but I haven't been able to extend it to solve my problem:
combining two data frames of different lengths.
I've included code below to get the two dataframes populated. If any one can help that would be much appreciated.
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
I think you can do it most easily by creating empty columns and then filling them conditionally
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
Edit: In this I'm assuming that you wanted to insert the ratings in rows marked START
这篇关于在特定位置组合两个不同长度的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!