Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: `ID : Coercing LHS to a list` in adding an ID column, why?

I have data

       N11.1 N22.2 N33.1 N44.1 N21.1 N31.1 N32.1
Sinus      1     0     0   0.0     0     0  12.0
ArrAHB     1     0     0   0.1     0     0  20.9

where I want to add an extra column ID with values Sinus and ArrAHB.

require(lattice)
Sinus<-c(1,0,0,0,0,0,12)
ArrAHB<-c(1,0,0,0.1,0,0,20.9)
Labels<-c("N11.1","N22.2","N33.1","N44.1","N21.1","N31.1","N32.1")
ID<-c("Sinus","Arr/AHB")
data.female<-data.frame(Sinus,ArrAHB,row.names=Labels)
data.female<-t(data.female)

> data.female$ID<-ID

Warning message:
In data.female$ID <- ID : Coercing LHS to a list

Why does the creation of the ID column cause the coercion in the data.frame?

P.s. My goal is to get this data to the form like here for barchart(N11.1+N22.1+N33.1+N44.1+N21.1+N31.1+N32.1 ~ ID, data=data.female) which requires a new ID column here, I cannot understand why this ID addition sometimes works and sometimes not. Please explain.

like image 745
hhh Avatar asked Nov 18 '16 16:11

hhh


2 Answers

It's throwing a warning, as the results of transpose t() are a matrix. Matricies don't have accessible column names. You have to coerce the matrix to a data frame before you make the ID assignment, using as.data.frame()

This works.

Sinus<-c(1,0,0,0,0,0,12)
ArrAHB<-c(1,0,0,0.1,0,0,20.9)
Labels<-c("N11.1","N22.2","N33.1","N44.1","N21.1","N31.1","N32.1")
ID<-c("Sinus","Arr/AHB")
data.female<-data.frame(Sinus,ArrAHB,row.names=Labels)
data.female<-as.data.frame(t(data.female))

data.female$ID<-ID

Remember that data frames are defined column-wise and not row-wise. A data frame definition should be by columns.

like image 173
Jake Kaupp Avatar answered Nov 09 '22 07:11

Jake Kaupp


I know this is late but I ran across the same problem. You can do this:

data.female <- cbind(data.female, ID)
like image 24
Denis Avatar answered Nov 09 '22 09:11

Denis