[英]Hi, I am trying to create an object in R and then subset the data but am getting an error message regarding dimensions
I am very new at R so I know the fix is simple, I would appreciate if someone could explain to me though my mistake and how to fix it.我是 R 的新手,所以我知道修复很简单,如果有人能向我解释我的错误以及如何修复它,我将不胜感激。
dat4<-c(10, 11)
subDat<-dat4[,c(10,11)]
The error that I am getting is "Error in subDat4<-dat4[,c(10,11)] incorrect number of dimensions"我得到的错误是“错误 subDat4<-dat4[,c(10,11)] 维数不正确”
Thank you in advance先感谢您
welcome to StackOverflow.欢迎使用 StackOverflow。 You are specifying the dat4 as a vector (one dimension object), but trying to subset as data.frame/tible (2 dimensional objects)...
您将 dat4 指定为向量(一维对象),但尝试将子集作为 data.frame/tible(二维对象)...
To specify dat4[a,b]
, with a
being the indication for rows, and b
a indication for columns, you need to have columns and rows (data frame, matrix, ...)要指定
dat4[a,b]
, a
是行的指示, b
是列的指示,您需要有列和行(数据框、矩阵等)
Your data is not a matrix, thus, you can not subset a vector as a matrix.您的数据不是矩阵,因此,您不能将向量子集化为矩阵。 You can only subset matrix with square bracket as you did.
您只能像以前一样使用方括号对矩阵进行子集化。
Try尝试
dat4<-c(10, 11)
dat5<-c(12, 13)
mat1<-matrix(c(dat4,dat5),nrow=2)
mat1[1,2]
# 12
You can see my subst states row one column two which prints 12, that is the element that falls on row one column two.您可以看到我的 subst 状态第一行第二列打印 12,即落在第一行第二列上的元素。
If you want to subset the vector you provided you can go this way.如果你想对你提供的向量进行子集化,你可以这样做。
dat4[[1]]
#[1] 10
That show the first element of the vector 'dat4' and显示向量 'dat4' 的第一个元素和
dat4[[2]]
#[ 11
Which show the second element of 'dat4'显示“dat4”的第二个元素
I hope this answer is of help to you.我希望这个答案对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.