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

Categories

0 votes
428 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...