mget与get无法理解它们之间的区别。

huangapple go评论90阅读模式
英文:

mget vs get unable to understand the difference

问题

我有点困惑mgetget的工作方式

我正在使用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 &lt;- tribble(
~City_Name,  ~Temp,  ~Pres,  ~Wind_Hor, ~Wind_Ver,  ~S_Moist,  ~Temp1, ~Pres1,  ~Wind1,  ~S_Moist1,
&#39;Mi&#39;,  27,  1019,  287,  278,  78,  1,  2,  2,  1,
&#39;Mi&#39;,  28,  1019,  289,  277,  78,  2,  2,  1,  3
)

df2 &lt;- tribble(
  ~City_Name,  ~Temp,  ~Pres,  ~Wind_Hor, ~Rainf,  ~S_Moist,  ~Temp1, ~Pres1,  ~Wind1,  ~S_Moist1,
  &#39;Mi&#39;,  27,  1019,  287,  278,  78,  1,  2,  2,  1,
  &#39;Mi&#39;,  28,  1019,  289,  277,  78,  2,  2,  1,  3
)

dfs1 &lt;- c(&#39;df1&#39;,&#39;df2&#39;)

var &lt;- c(&#39;City_Name&#39;,  &#39;Temp&#39;,  &#39;Pres&#39; , &#39;Wind_Hor&#39; , &#39;Wind_Ver&#39; , &#39;Rainf&#39; , &#39;S_Moist&#39;)

lapply(dfs1, \(x) {
  dfn &lt;- mget(x, envir = .GlobalEnv, inherits = T)
  dfn[[var[which(is.na(match(var,names(dfn))))]]] &lt;- NA
  dfn &lt;- dfn %&gt;% select(all_of(var))
  return(assign(x,dfn,envir = .GlobalEnv))
})

答案1

得分: 3

get 用于查找一个变量并返回存储在变量中的值,而 mget 查找多个变量并将它们返回为一个列表。您试图要做的事情可以简化为:

lapply(mget(dfs1), \(x) `[&lt;-`(x, setdiff(var, names(x)), value = NA)[var]) |&gt;
   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) `[&lt;-`(x, setdiff(var, names(x)), value = NA)[var]) |&gt;
   list2env(.GlobalEnv)


df1
# A tibble: 2 &#215; 7
  City_Name  Temp  Pres Wind_Hor Wind_Ver Rainf S_Moist
  &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt; &lt;lgl&gt;   &lt;dbl&gt;
1 Mi           27  1019      287      278 NA         78
2 Mi           28  1019      289      277 NA         78

df2
# A tibble: 2 &#215; 7
  City_Name  Temp  Pres Wind_Hor Wind_Ver Rainf S_Moist
  &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;lgl&gt;    &lt;dbl&gt;   &lt;dbl&gt;
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 &lt;- tribble(
  ~City_Name,  ~Temp,  ~Pres,  ~Wind_Hor, ~Wind_Ver,  ~S_Moist,  ~Temp1, ~Pres1,  ~Wind1,  ~S_Moist1,
  &#39;Mi&#39;,  27,  1019,  287,  278,  78,  1,  2,  2,  1,
  &#39;Mi&#39;,  28,  1019,  289,  277,  78,  2,  2,  1,  3
)

df2 &lt;- tribble(
  ~City_Name,  ~Temp,  ~Pres,  ~Wind_Hor, ~Rainf,  ~S_Moist,  ~Temp1, ~Pres1,  ~Wind1,  ~S_Moist1,
  &#39;Mi&#39;,  27,  1019,  287,  278,  78,  1,  2,  2,  1,
  &#39;Mi&#39;,  28,  1019,  289,  277,  78,  2,  2,  1,  3
)

dfs1 &lt;- c(&#39;df1&#39;,&#39;df2&#39;)

var &lt;- c(&#39;City_Name&#39;,  &#39;Temp&#39;,  &#39;Pres&#39; , &#39;Wind_Hor&#39; , &#39;Wind_Ver&#39; , &#39;Rainf&#39; , &#39;S_Moist&#39;)

# get all data sets in one call to mget
df_list &lt;- mget(dfs1, envir = .GlobalEnv, inherits = TRUE)
lapply(df_list, \(x) {
  icol &lt;- var[!var %in% names(x)]
  x[icol] &lt;- NA
  x[var]
}) |&gt;
  list2env(envir = .GlobalEnv)
#&gt; &lt;environment: R_GlobalEnv&gt;

df1
#&gt; # A tibble: 2 &#215; 7
#&gt;   City_Name  Temp  Pres Wind_Hor Wind_Ver Rainf S_Moist
#&gt;   &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt; &lt;lgl&gt;   &lt;dbl&gt;
#&gt; 1 Mi           27  1019      287      278 NA         78
#&gt; 2 Mi           28  1019      289      277 NA         78

df2
#&gt; # A tibble: 2 &#215; 7
#&gt;   City_Name  Temp  Pres Wind_Hor Wind_Ver Rainf S_Moist
#&gt;   &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;lgl&gt;    &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 Mi           27  1019      287 NA         278      78
#&gt; 2 Mi           28  1019      289 NA         277      78

<sup>Created on 2023-07-17 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月17日 12:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定