Rohan Singhal
Rohan Singhal

Reputation: 33

Copying one data frame into another

I am trying to add the data from one data frame into a much larger data frame. Basically, in my case "df" is a data frame where the row lengths are 376, but I want to put these 376 items into "data" which will have row lengths of 10000. The reason I am doing this is because while my data in "df" is capped at 376 items per row, each row is not complete and I am going to concatenate rows together once I get them into the larger "data" data frame. The issue with my code shown below is that when I try transferring a row of "df" into a row of "data", I get numbers instead of some letter name that should be shown in place (I'm assuming the numbers are the location of the item in memory). How do I fix my code so that I get the actual item names transferred over to "data"?

df<-read.table("msigdb.v5.2.symbols.txt", fill = TRUE)
data<-data.frame(matrix(NA,ncol = 10000, nrow = 19337))
for (w in 1:20){
data[w,]<-df[w,]
}

data[1,1] should be "AAANWWTGC_UNKNOWN", instead I am just getting "5"

Upvotes: 1

Views: 59

Answers (1)

nadizan
nadizan

Reputation: 1373

That is due to that you are replacing a row in a data.frame that contain factors to a data.frame that contains logical values, and the default will be to convert the df values to integers.

Look at the two examples below:

1) matrix to data.frame

data<-data.frame(matrix(NA,ncol = 3, nrow = 3))
df<-(matrix(data=rep(c("pos1","pos2",3),3),nrow=3,byrow=FALSE))

str(df)
 chr [1:3, 1:3] "pos1" "pos2" "3" "pos1" "pos2" "3" "pos1" "pos2" "3"

str(data)
 'data.frame':  3 obs. of  3 variables:
 $ X1: logi  NA NA NA
 $ X2: logi  NA NA NA
 $ X3: logi  NA NA NA

data[1,]<-df[1,]

str(data)
'data.frame':   3 obs. of  3 variables:
 $ X1: chr  "pos1" NA NA
 $ X2: chr  "pos1" NA NA
 $ X3: chr  "pos1" NA NA

2) data.frame to data.frame

data<-data.frame(matrix(NA,ncol = 3, nrow = 3))
df<-data.frame(matrix(data=rep(c("pos1","pos2",3),3),nrow=3,byrow=FALSE))

str(df)
'data.frame':   3 obs. of  3 variables:
 $ X1: Factor w/ 3 levels "3","pos1","pos2": 2 3 1
 $ X2: Factor w/ 3 levels "3","pos1","pos2": 2 3 1
 $ X3: Factor w/ 3 levels "3","pos1","pos2": 2 3 1

str(data)
'data.frame':   3 obs. of  3 variables:
 $ X1: logi  NA NA NA
 $ X2: logi  NA NA NA
 $ X3: logi  NA NA NA

data[1,]<-df[1,]

str(data)
'data.frame':   3 obs. of  3 variables:
 $ X1: int  2 NA NA
 $ X2: int  2 NA NA
 $ X3: int  2 NA NA

Upvotes: 1

Related Questions