用于提取 ESS 数据的 R 函数,"could not find function paste"
R function for extracting ESS data, "could not find function paste"
从 "essurvey" 包中,我有一个数据集(作为 "list" 对象),我试图从中获取所有国家/地区的所有 ESS 回合。为了稍微有效地做到这一点,我试图创建一个函数来为我做这件事,但是我遇到了问题:
首先,如果我使用 "paste" 函数获取对象的名称,则会出现错误:
Error in paste: could not find function "paste<-"
。
其次,当尝试使用函数设置名称只是为了查看它是否可以在没有粘贴部分的情况下工作时,它运行时没有错误,但也没有任何结果。谁能看到解决方案?我感谢所有帮助!
ESS_Load <- function(N, CC){
temp <- Data[[N]]
paste(N, CC, sep = "_") <-
subset(temp, cntry == CC)
rm(temp)
}
ESS_Load(9, "NO")
据我从您的评论中了解到,您想在调用环境中创建一个名为 {CC}_{N}
的数据框,作为函数的副作用。
可以这样实现:
# first build sample data
set.seed(1)
Data <- list(iris[sample(nrow(iris),5),], iris[sample(nrow(iris),5),])
Data
#> [[1]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 68 5.8 2.7 4.1 1.0 versicolor
#> 129 6.4 2.8 5.6 2.1 virginica
#> 43 4.4 3.2 1.3 0.2 setosa
#> 14 4.3 3.0 1.1 0.1 setosa
#> 51 7.0 3.2 4.7 1.4 versicolor
#>
#> [[2]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 85 5.4 3.0 4.5 1.5 versicolor
#> 21 5.4 3.4 1.7 0.2 setosa
#> 106 7.6 3.0 6.6 2.1 virginica
#> 74 6.1 2.8 4.7 1.2 versicolor
#> 7 4.6 3.4 1.4 0.3 setosa
iris_Load <- function(N, CC){
nm <- paste(CC, N, sep = "_")
res <- subset(Data[[N]], Species == CC)
assign(nm, res, envir = parent.frame()) # create your object with right name and value in calling environment
invisible() # will return NULLL without printing
}
iris_Load(1, "setosa")
setosa_1
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 43 4.4 3.2 1.3 0.2 setosa
#> 14 4.3 3.0 1.1 0.1 setosa
如果您能负担得起执行以下操作,这可以说是更好的做法,具有副作用的函数可能会令人惊讶,并且在大多数情况下通常不鼓励使用:
iris_Load <- function(N, CC){
subset(Data[[N]], Species == CC)
}
setosa_1 <- iris_Load(1, "setosa")
setosa_1
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 43 4.4 3.2 1.3 0.2 setosa
#> 14 4.3 3.0 1.1 0.1 setosa
```
从 "essurvey" 包中,我有一个数据集(作为 "list" 对象),我试图从中获取所有国家/地区的所有 ESS 回合。为了稍微有效地做到这一点,我试图创建一个函数来为我做这件事,但是我遇到了问题:
首先,如果我使用 "paste" 函数获取对象的名称,则会出现错误:
Error in paste: could not find function "paste<-"
。
其次,当尝试使用函数设置名称只是为了查看它是否可以在没有粘贴部分的情况下工作时,它运行时没有错误,但也没有任何结果。谁能看到解决方案?我感谢所有帮助!
ESS_Load <- function(N, CC){
temp <- Data[[N]]
paste(N, CC, sep = "_") <-
subset(temp, cntry == CC)
rm(temp)
}
ESS_Load(9, "NO")
据我从您的评论中了解到,您想在调用环境中创建一个名为 {CC}_{N}
的数据框,作为函数的副作用。
可以这样实现:
# first build sample data
set.seed(1)
Data <- list(iris[sample(nrow(iris),5),], iris[sample(nrow(iris),5),])
Data
#> [[1]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 68 5.8 2.7 4.1 1.0 versicolor
#> 129 6.4 2.8 5.6 2.1 virginica
#> 43 4.4 3.2 1.3 0.2 setosa
#> 14 4.3 3.0 1.1 0.1 setosa
#> 51 7.0 3.2 4.7 1.4 versicolor
#>
#> [[2]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 85 5.4 3.0 4.5 1.5 versicolor
#> 21 5.4 3.4 1.7 0.2 setosa
#> 106 7.6 3.0 6.6 2.1 virginica
#> 74 6.1 2.8 4.7 1.2 versicolor
#> 7 4.6 3.4 1.4 0.3 setosa
iris_Load <- function(N, CC){
nm <- paste(CC, N, sep = "_")
res <- subset(Data[[N]], Species == CC)
assign(nm, res, envir = parent.frame()) # create your object with right name and value in calling environment
invisible() # will return NULLL without printing
}
iris_Load(1, "setosa")
setosa_1
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 43 4.4 3.2 1.3 0.2 setosa
#> 14 4.3 3.0 1.1 0.1 setosa
如果您能负担得起执行以下操作,这可以说是更好的做法,具有副作用的函数可能会令人惊讶,并且在大多数情况下通常不鼓励使用:
iris_Load <- function(N, CC){
subset(Data[[N]], Species == CC)
}
setosa_1 <- iris_Load(1, "setosa")
setosa_1
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 43 4.4 3.2 1.3 0.2 setosa
#> 14 4.3 3.0 1.1 0.1 setosa
```