Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

For example

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...