4
votes

I wrote a script in R that should fuse two data.frames containing both string (stored as factors with levels) and integer columns into one data.frame and write it to a new row in a previously defined data.frame (after different for if statements)

 new_frame <- data.frame(matrix(ncol=(ncol(X)+ncol(y)),nrow=0))
 colnames(new_frame) <- c(colnames(X), colnames(Y))

 new_frame[nrow(new_frame)+1,] <- data.frame(c(data.frame(X[j,]),data.frame(Y[i,])

Unfortunately the newly copied data do only show integer values, even for the string columns (I guess the factors are directly converted into integers), but I need the levels for the string columns.

Weirdly the script worked perfectly fine last week, and without changing anything to script or the data files but now directly converts everything to integer.

Any help would be highly appreciated! Thanks very much! best k

1
This is a fail safe for working with factors. You could coerce these variables to characters before appending them. That being said. there is probably a better way of resolving what you're doing using lapply, sapply, do.call...Roman Luštrik
you have a lower case y on the first line, and a missing parenthesis on your last lineMoody_Mudskipper
@Moody_Mudskipper thank's very much, this works perfectly fine!kai k.
Glad I could help, I've put my comment as an answer.Moody_Mudskipper

1 Answers

0
votes

As @Roman said there's probably better ways to do what you're trying to do, but I think rbind can be a quick fix:

new_frame <- rbind(new_frame,data.frame(X[j,],Y[i,]))