Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy row from one data.frame in to another [R]

Tags:

copy

dataframe

r

There are two data frames x, y . Id like to copy row with number J from X into Y. Something like

    Y[1,] <- X[j,]
like image 372
David Kakauridze Avatar asked Apr 04 '12 15:04

David Kakauridze


1 Answers

Your example happens to pretty much answer your question. (Try it out!)

If, instead of replacing a row in the target data.frame, you want to add a row to it, try rbind() instead:

X <- data.frame(name=LETTERS[1:3], value=1:3, stringsAsFactors=FALSE)
Y <- data.frame(name=letters[1:3], value=rnorm(3), stringsAsFactors=FALSE)

X[1,] <- Y[1,]
X <- rbind(X, Y[3,])
like image 159
Josh O'Brien Avatar answered Sep 19 '22 14:09

Josh O'Brien