简体   繁体   English

嗨,我正在尝试在 R 中创建一个对象,然后对数据进行子集化,但收到有关维度的错误消息

[英]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.

相关问题 嗨,我正在尝试将数据从 nasdaq 输入到 R 中,但出现错误 - Hi, I am trying to input data from nasdaq into R and am getting an error 嗨,我是 r 的新手,并试图在 r 中使用 grad - Hi I am new in r and trying to use grad in r 当我尝试在 r 中制作直方图时,为什么会收到一条错误消息说 x 不是数字 - Why am I getting an error message saying x is not numeric when I'm trying to make a histogram in r 尝试在 R 中使用 for 循环来创建图形,但收到错误消息 - Attempting to use for loop in R to create graphs but am getting error message R 中的 poLCA 未执行。 我收到一条奇怪的错误消息 - poLCA in R is not executing. I am getting a weird error message 我正在尝试对 R 运行逻辑回归并收到此错误 - I'm trying to run a logistic regression on R and am getting this error 尝试重命名 R 中的列,但出现错误 - Trying to rename a column in R but i am getting an error 我正在尝试加载 tidyverse 库,但收到一条错误消息 - I am trying to load the tidyverse library, but I am getting an error message 我正在尝试将 SAS 代码转换为 R,但我不断收到错误“错误:在“} 中出现意外的‘}’” - I am trying to convert SAS code to R, but I am continuously getting an error "Error: unexpected '}' in " }" R。 我正在尝试将我的数据框子集几十年。 因此,我想通过使用列的值进行子集化 - R. I am trying to subset my data frame by decades. Therefore I want to subset by using values of a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM