英文:
mget vs get unable to understand the difference
问题
我有点困惑mget
和get
的工作方式
我正在使用lapply
来读取多个数据框(df1,df2)并进行操作,但是当我使用get
时,对象被读取,但如果我用mget
替换它,它不起作用,不知道为什么
我注意到的是,如果我使用get
,数据框会在全局环境中搜索并作为数据框读取,但如果我使用mget
,那么对象会作为列表读取,因此后续的代码无法正常工作
df1 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
df2 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
dfs1 <- c('df1', 'df2')
var <- c('City_Name', 'Temp', 'Pres', 'Wind_Hor', 'Wind_Ver', 'Rainf', 'S_Moist')
lapply(dfs1, \(x) {
dfn <- mget(x, envir = .GlobalEnv, inherits = T)
dfn[[var[which(is.na(match(var, names(dfn))))]]] <- NA
dfn <- dfn %>% select(all_of(var))
return(assign(x, dfn, envir = .GlobalEnv))
})
英文:
I am little confused with the way the mget
and get
works
I am using the lapply
to read the multiple dataframes (df1, df2) and do the manipulation, however when i use the get
the object is read but if i replace it with mget
, it does not work, not sure why
what i noticed is that if i use the get
the dataframe is searched in the global environment and read as a dataframe, however if I use mget
then the object is read as a list due to which the further code does not work
df1 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
df2 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
dfs1 <- c('df1','df2')
var <- c('City_Name', 'Temp', 'Pres' , 'Wind_Hor' , 'Wind_Ver' , 'Rainf' , 'S_Moist')
lapply(dfs1, \(x) {
dfn <- mget(x, envir = .GlobalEnv, inherits = T)
dfn[[var[which(is.na(match(var,names(dfn))))]]] <- NA
dfn <- dfn %>% select(all_of(var))
return(assign(x,dfn,envir = .GlobalEnv))
})
答案1
得分: 3
get
用于查找一个变量并返回存储在变量中的值,而 mget
查找多个变量并将它们返回为一个列表。您试图要做的事情可以简化为:
lapply(mget(dfs1), \(x) `[<-`(x, setdiff(var, names(x)), value = NA)[var]) |>
list2env(.GlobalEnv)
df1
一个数据框: 2 × 7
城市名称 温度 压力 水平风 垂直风 降雨 土壤湿度
<chr> <dbl> <dbl> <dbl> <dbl> <lgl> <dbl>
1 Mi 27 1019 287 278 NA 78
2 Mi 28 1019 289 277 NA 78
df2
一个数据框: 2 × 7
城市名称 温度 压力 水平风 垂直风 降雨 土壤湿度
<chr> <dbl> <dbl> <dbl> <lgl> <dbl> <dbl>
1 Mi 27 1019 287 NA 278 78
2 Mi 28 1019 289 NA 277 78
英文:
get
is used to find only one variable and return the value stored in the variable, while mget
finds multiple variable and returns them in a list. What you are trying to do can be simplified as
lapply(mget(dfs1), \(x) `[<-`(x, setdiff(var, names(x)), value = NA)[var]) |>
list2env(.GlobalEnv)
df1
# A tibble: 2 × 7
City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
<chr> <dbl> <dbl> <dbl> <dbl> <lgl> <dbl>
1 Mi 27 1019 287 278 NA 78
2 Mi 28 1019 289 277 NA 78
df2
# A tibble: 2 × 7
City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
<chr> <dbl> <dbl> <dbl> <lgl> <dbl> <dbl>
1 Mi 27 1019 287 NA 278 78
2 Mi 28 1019 289 NA 277 78
答案2
得分: 2
以下是使用mget
的解决方案。从帮助页面?get
中得到以下信息:
按名称搜索一个对象(get)或零个或多个对象(mget)。
在“Value”部分中:
对于
get
,找到的对象。如果没有找到对象,将产生错误。对于
mget
,一个对象的命名列表(找到或通过ifnotfound
指定)。
suppressPackageStartupMessages(
library(tibble)
)
df1 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
df2 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
dfs1 <- c('df1','df2')
var <- c('City_Name', 'Temp', 'Pres' , 'Wind_Hor' , 'Wind_Ver' , 'Rainf' , 'S_Moist')
# 使用mget一次获取所有数据集
df_list <- mget(dfs1, envir = .GlobalEnv, inherits = TRUE)
lapply(df_list, \(x) {
icol <- var[!var %in% names(x)]
x[icol] <- NA
x[var]
}) ->
list2env(envir = .GlobalEnv)
# <environment: R_GlobalEnv>
df1
# # A tibble: 2 × 7
# City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
# <chr> <dbl> <dbl> <dbl> <dbl> <lgl> <dbl>
# 1 Mi 27 1019 287 278 NA 78
# 2 Mi 28 1019 289 277 NA 78
df2
# # A tibble: 2 × 7
# City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
# <chr> <dbl> <dbl> <dbl> <lgl> <dbl> <dbl>
# 1 Mi 27 1019 287 NA 278 78
# 2 Mi 28 1019 289 NA 277 78
创建于2023年7月17日,使用reprex v2.0.2
英文:
Here is a solution with mget
.
From the help page ?get
:
> Search by name for an object (get) or zero or more objects (mget).
And in section Value:
> For get
, the object found. If no object is found an error results.
>
> For mget
, a named list of objects (found or specified via ifnotfound
).
suppressPackageStartupMessages(
library(tibble)
)
df1 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
df2 <- tribble(
~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
)
dfs1 <- c('df1','df2')
var <- c('City_Name', 'Temp', 'Pres' , 'Wind_Hor' , 'Wind_Ver' , 'Rainf' , 'S_Moist')
# get all data sets in one call to mget
df_list <- mget(dfs1, envir = .GlobalEnv, inherits = TRUE)
lapply(df_list, \(x) {
icol <- var[!var %in% names(x)]
x[icol] <- NA
x[var]
}) |>
list2env(envir = .GlobalEnv)
#> <environment: R_GlobalEnv>
df1
#> # A tibble: 2 × 7
#> City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
#> <chr> <dbl> <dbl> <dbl> <dbl> <lgl> <dbl>
#> 1 Mi 27 1019 287 278 NA 78
#> 2 Mi 28 1019 289 277 NA 78
df2
#> # A tibble: 2 × 7
#> City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
#> <chr> <dbl> <dbl> <dbl> <lgl> <dbl> <dbl>
#> 1 Mi 27 1019 287 NA 278 78
#> 2 Mi 28 1019 289 NA 277 78
<sup>Created on 2023-07-17 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论