R:`ID : Coercing LHS to a list` 添加一个 ID 列,为什么? [英] R: `ID : Coercing LHS to a list` in adding an ID column, why?

查看:128
本文介绍了R:`ID : Coercing LHS to a list` 添加一个 ID 列,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据

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

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

要求(格子)正弦<-c(1,0,0,0,0,0,12)ArrAHB<-c(1,0,0,0.1,0,0,20.9)标签<-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警告信息:在 data.female$ID <- ID 中:强制 LHS 到一个列表

<块引用>

为什么创建ID列会导致data.frame中的强制转换?

P.s.我的目标是将这些数据转换为类似于 here 的表单,用于 barchart(N11.1+N22.1+N33.1+N44.1+N21.1+N31.1+N32.1 ~ ID, data=data.female) 这需要一个新的 ID 列 此处,我无法理解为什么此 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)标签<-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

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

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.

解决方案

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.

这篇关于R:`ID : Coercing LHS to a list` 添加一个 ID 列,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆