简体   繁体   English

R循环功能怎么了?

[英]whats wrong with this R loop function?

I am trying to run a loop function like this: 我试图运行这样的循环功能:

x <- c(1,2,3,4,7,8,9,10,13,14,15,16,19,20,22,23,24,26) 
n<-rep(NA,length(x))
for (ii in 1:length(x)){
  n[ii]<-subset(dat1, Plot==ii)
}

However, error message appeared: 但是,出现错误信息:

There were 18 warnings (use warnings() to see them)

The outputs that I am looking for is 18 individual data n1,n2,n3,n4,n7,...n24&n26 subset from main data dat1 . 我正在寻找的输出是来自主数据dat1 18个单独数据n1,n2,n3,n4,n7,... n24&n26子集。

I'm having a little trouble understanding what you want to do, but if you have a vector dat1 , and you want to form a new vector n that contains the 1st, 2nd, ..., 26th elements of dat1 , that's pretty easy to do in R. Try: 我在理解要执行的操作时遇到了一些麻烦,但是如果您有一个向量dat1 ,并且您想要形成一个新的向量n ,该向量包含dat1的第1、2,...,26个元素,那很容易在R中进行尝试。

> dat1 <- 1:60
> x <- c(1,2,3,4,7,8,9,10,13,14,15,16,19,20,22,23,24,26)
> n <- dat1[x]
> n
 [1]  1  2  3  4  7  8  9 10 13 14 15 16 19 20 22 23 24 26

If what you're trying to do is create a "vector" of data.frames, each one corresponding to a certain level of your Plot variable, you should be using a list , not a vector . 如果您要创建的是data.frames的“向量”,每个对应于Plot变量的特定级别,则应该使用list ,而不是vector

x <- c(1,2,3,4,7,8,9,10,13,14,15,16,19,20,22,23,24,26) 
n <- list()
length(n) <- length(x)

for (ii in 1:length(x)){
  n[[ii]] <- subset(dat1, Plot==ii)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM