Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

r - Append data frame to another data frame with different column names

I'm trying to add one data frame as rows to another data frame. The column names don't match, so I have a lookup table that matches them. I thought I would be able to do each column one at a time in a loop, but I keep getting replacement has 209 rows, data has 157 error.

How can I add them all at once? Here's an example of what I'm trying to do:

I've got two data sets

test1 = c("one", "two", "three", "four", "five")
test2 = c("1", "3", "4", "5")

And a lookup table to match the column names

first second
1   one      1
2 three      3
3  four      4
4  five      5

So I'd like to append all the rows from "1" into "one", "3" into "three" etc. I've tried a few different things and gotten errors either about the number of rows or the fact that I don't have replacements for each value, like in my example I'm ignoring the column "two".

Could anyone recommend how to do this?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Is there any reason you're opposed to setting the names of the two sets to be the same?

rbind will work just fine in that case, and you can just use something like names(test2) <- lookup_table$first so that the names match up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...