[英]wrote function in R, receiving error “Error in eval(expr, envir, enclos) : …”
writing code for homework. 为家庭作业编写代码。 The code goes in fine, but when I try the function, I receive this #'ed error (which I do not understand).
代码很好,但是当我尝试这个函数时,我收到了这个#'ded错误(我不明白)。 The code is supposed to take a dataframe of coordinates, keep the coords, and add columns denoting which hemisphere the coordinates is part of.
该代码应该采用坐标的数据框,保持坐标,并添加表示坐标所属的半球的列。 So the question is how do I change the function/code to get rid of the error?
所以问题是如何更改函数/代码以消除错误?
Also, the code uses packages sp and foreach. 此外,代码使用包sp和foreach。
Here's the dataframe input and my errored output: 这是数据帧输入和我的错误输出:
set.seed(10)
n <- 10
df <- data.frame(xpos=runif(n,0,360),ypos=runif(n,-90,90))
df
outHemisphere <- hemisphereSummary(df=df)
outHemisphere
# Error in eval(expr, envir, enclos) : argument is missing, with no default
Here's what the output is supposed to be (after the set seed and df are in workspace): 这是输出应该是什么(在设置种子和df在工作空间之后):
outHemisphere <- hemisphereSummary(df=df)
outHemisphere
# coordinates EWhemisphere NShemisphere
#1 (182.692, 27.298) W N
#2 (110.437, 12.1928) E N
#3 (153.687, -69.5684) E S
#4 (249.517, 17.2666) W N
#5 (30.6489, -25.551) E S
#6 (81.1572, -12.8143) E S
#7 (98.831, -80.6574) E S
#8 (98.0298, -42.448) E S
#9 (221.699, -18.2177) W S
#10 (154.682, 60.5041) E N
Here's my code/functions for the assignment, which inputs without errors: 这是我的分配代码/函数,输入没有错误:
hemisphereSummary <- function(df, projargs="+proj=longlat +datum=WGS84")
{
# install.packages("foreach")
# install.packages("sp")
library("foreach")
library("sp")
if(class(df)!="data.frame") stop ("df must be a data frame.")
registerDoSEQ() # register the non-parallel backend for foreach.
df_mat <- cbind(df[,1], df[,2]) #makes matrix of 2 cols, drawing from df.
row.names(df_mat) <- as.character(1:nrow(df_mat))
df_CRS <- CRS(projargs) # correct projection arguments.
df_sp <- SpatialPoints(coords=df_mat,proj4string=df_CRS) # makes spatial object!
# df_sp
foreachloop <- foreach(i = 1:(nrow(df)), .packages = "sp", .combine="rbind",) %dopar%
{
findhemisphere <- function(i, df_sp, df) # nested function to sort which hemi.
{
coords <- coordinates(df_sp[i,])
hemi_names <- df
colnames(hemi_names) <- c("EWhemisphere", "NShemisphere")
#if-else's: E=0-180, W=181-360. N=0 to +90, S=-1 to -90.
ifelse(coords[1] >=0 & coords[1] <=180,coords[1] <- "E", coords[1] <- "W")
ifelse(coords[2] >=0 & coords[2] <=90,coords[2] <- "N", coords[2] <- "S")
return(hemi_names)[i,] <- coords
}
}
SPdataframe <- SpatialPointsDataFrame(coords=coordinates(df_sp),
data = foreachloop, proj4string = df_CRS, match.ID = FALSE)
}
The error: 错误:
Error in eval(expr, envir, enclos) : argument is missing, with no default
is caused by the trailing comma in the call to foreach
since that is interpreted as a missing argument to foreach
. 是由
foreach
调用中的尾随逗号引起的,因为它被解释为foreach
的缺失参数。 But you'll run into more problems after you remove it. 但是在删除它之后会遇到更多问题。 For example, the
foreach
loop is defining, not calling, the same function repeatedly and combining those functions with rbind
. 例如,
foreach
循环重复定义而不是调用相同的函数,并将这些函数与rbind
组合。 Presumably you want to define the function outside the loop, call it in the loop, and combine the results with rbind
. 大概你想在循环外定义函数,在循环中调用它,并将结果与
rbind
结合起来。
I'm also skeptical about the last line of the foreach loop: 我也对foreach循环的最后一行持怀疑态度:
return(hemi_names)[i,] <- coords
But I don't think there's any reason to use foreach
for this. 但我认为没有任何理由为此使用
foreach
。 I believe the coordinates
function can operate on the entire object df_sp
, so there's no reason for a loop at all. 我相信
coordinates
函数可以对整个对象df_sp
,因此根本没有理由进行循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.