R: `ID : Coercing LHS to a list` 添加 ID 列,为什么?

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

我有数据

       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

我想在其中添加一个额外的列 ID,其值为 SinusArrAHB

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。我的目标是把这个数据变成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 这样的形式,我不明白为什么这个ID加法有时有效有时无效。请解释。

这是一个警告,因为转置 t() 的结果是一个矩阵。矩阵没有可访问的列名。在进行 ID 分配之前,您必须使用 as.data.frame()

将矩阵强制转换为数据框

这有效。

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

请记住,数据框是按列定义的,而不是按行定义的。数据框定义应该按列。

我知道这已经晚了,但我 运行 遇到了同样的问题。你可以这样做:

data.female <- cbind(data.female, ID)