R中的循环表现出意想不到的效果 [英] for loop in R behaves in unexpected way
问题描述
我正在写这个函数,我的循环表现方式不同。你可以解释为什么这样做以及如何纠正?
完成< - function(directory,id = 1:332) {
specd< - list.files(specdata,full.names = TRUE)
temp< - vector(mode =numeric,(length = length(id)))
(for i){
temp [i] < - nrow(na.omit(read.csv(specd [i])))
}
return(data.frame (id = id,nobs = temp))
}
code:
complete(specdata,1)OBSERVATION - id = 1;得到1个答案
id nobs
1 1 117
complete(specdata,3)OBSERVATION - id = 3;得出3个答案
id nobs
1 3 0
2 3 NA
3 3 243
complete(specdata,30:25)OBSERVATION - id = 30; (specdata,c(2,4,8,10,12))
在data.frame(id = id,nobs
= temp):参数暗示不同行数的情况下显示跟踪重新运行错误:5,12
试试这个:
pre> complete < - function(directory,id = 1:332){
specd< - list.files(specdata,full.names = TRUE)
temp < c()
for(i in id){
temp <-c(temp,nrow(na.omit(read.csv(specd [i]))))
}
return(data.frame(id = id,nobs = temp))
}
你得到不匹配的行长度的原因是因为 temp [i]
将输出分配给temp中的第i个地标。所以当你尝试 c(2,4,8,10,12)
。您期望得到 temp
的长度为5的向量。但你得到一个长度为12的向量。因为 temp [12]
输出的一个元素。所以 temp
被拉伸到这个长度。示例:
x < - 1
x
[1] 1
x [5] < - 2
x
[1] 1不适用不适用2
我给x的第五个元素赋了一个值,R扩展了矢量而不要求我去做任务。完成(specdata,3)返回3个答案,因为 temp
最初有一个值, 0
。您在开始时预先指定了它。然后按照你的指示,把for循环分配给 243
到 temp [3]
。所以R为第二个值填充了一个NA值,剩下三个。
id nobs
1 3 0
2 3 NA
3 3 243
I'm writing this function and my loop is behaving in a different way. can you explain why it does this and how to correct?
complete <- function(directory, id = 1:332) {
specd <- list.files("specdata", full.names = TRUE)
temp<- vector(mode = "numeric", (length = length(id)))
for (i in id) {
temp[i] <- nrow(na.omit(read.csv(specd[i])))
}
return(data.frame(id = id, nobs = temp))
}
code:
complete("specdata", 1)OBSERVATION – id = 1; yields 1 answer
id nobs
1 1 117
complete("specdata", 3) OBSERVATION – id = 3; yields 3 answers
id nobs
1 3 0
2 3 NA
3 3 243
complete("specdata", 30:25) OBSERVATION – id = 30; yields 30 answers
complete("specdata", c(2, 4, 8, 10, 12))
Show Traceback Rerun with Debug Error in data.frame(id = id, nobs = temp) : arguments imply differing number of rows: 5, 12
Try this:
complete <- function(directory, id = 1:332) {
specd <- list.files("specdata", full.names = TRUE)
temp<- c()
for (i in id) {
temp <- c(temp,nrow(na.omit(read.csv(specd[i]))))
}
return(data.frame(id = id, nobs = temp))
}
The reason you are getting mismatched row lengths is because temp[i]
assigns the output to the ith placemark in temp. So when you try c(2, 4, 8, 10, 12)
. You are expecting to get a vector of length five for temp
. but you get a vector of length 12. Because temp[12]
an element of the output. So temp
is stretched out to that length. Example:
x <- 1
x
[1] 1
x[5] <- 2
x
[1] 1 NA NA NA 2
When I assigned a value to the fifth element of x, R expanded the vector without asking me to do the task.
complete("specdata", 3) returned 3 answers because temp
initially had one value, 0
. You preassigned it at the beginning. Then the for loop assigned 243
to temp[3]
as you instructed it to. So R filled in an NA value for the second value, and you were left with three.
id nobs
1 3 0
2 3 NA
3 3 243
这篇关于R中的循环表现出意想不到的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!