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

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

mget vs get unable to understand the difference

问题

我有点困惑mgetget的工作方式

我正在使用lapply来读取多个数据框(df1,df2)并进行操作,但是当我使用get时,对象被读取,但如果我用mget替换它,它不起作用,不知道为什么

我注意到的是,如果我使用get,数据框会在全局环境中搜索并作为数据框读取,但如果我使用mget,那么对象会作为列表读取,因此后续的代码无法正常工作

  1. df1 <- tribble(
  2. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  3. 'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  4. 'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
  5. )
  6. df2 <- tribble(
  7. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  8. 'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  9. 'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
  10. )
  11. dfs1 <- c('df1', 'df2')
  12. var <- c('City_Name', 'Temp', 'Pres', 'Wind_Hor', 'Wind_Ver', 'Rainf', 'S_Moist')
  13. lapply(dfs1, \(x) {
  14. dfn <- mget(x, envir = .GlobalEnv, inherits = T)
  15. dfn[[var[which(is.na(match(var, names(dfn))))]]] <- NA
  16. dfn <- dfn %>% select(all_of(var))
  17. return(assign(x, dfn, envir = .GlobalEnv))
  18. })
英文:

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

  1. df1 &lt;- tribble(
  2. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  3. &#39;Mi&#39;, 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  4. &#39;Mi&#39;, 28, 1019, 289, 277, 78, 2, 2, 1, 3
  5. )
  6. df2 &lt;- tribble(
  7. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  8. &#39;Mi&#39;, 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  9. &#39;Mi&#39;, 28, 1019, 289, 277, 78, 2, 2, 1, 3
  10. )
  11. dfs1 &lt;- c(&#39;df1&#39;,&#39;df2&#39;)
  12. 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;)
  13. lapply(dfs1, \(x) {
  14. dfn &lt;- mget(x, envir = .GlobalEnv, inherits = T)
  15. dfn[[var[which(is.na(match(var,names(dfn))))]]] &lt;- NA
  16. dfn &lt;- dfn %&gt;% select(all_of(var))
  17. return(assign(x,dfn,envir = .GlobalEnv))
  18. })

答案1

得分: 3

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

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

  1. lapply(mget(dfs1), \(x) `[&lt;-`(x, setdiff(var, names(x)), value = NA)[var]) |&gt;
  2. list2env(.GlobalEnv)
  3. df1
  4. # A tibble: 2 &#215; 7
  5. City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
  6. &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dbl&gt;
  7. 1 Mi 27 1019 287 278 NA 78
  8. 2 Mi 28 1019 289 277 NA 78
  9. df2
  10. # A tibble: 2 &#215; 7
  11. City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
  12. &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dbl&gt; &lt;dbl&gt;
  13. 1 Mi 27 1019 287 NA 278 78
  14. 2 Mi 28 1019 289 NA 277 78

答案2

得分: 2

以下是使用mget的解决方案。从帮助页面?get中得到以下信息:

按名称搜索一个对象(get)或零个或多个对象(mget)。

在“Value”部分中:

对于get,找到的对象。如果没有找到对象,将产生错误。

对于mget,一个对象的命名列表(找到或通过ifnotfound指定)。

  1. suppressPackageStartupMessages(
  2. library(tibble)
  3. )
  4. df1 <- tribble(
  5. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  6. 'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  7. 'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
  8. )
  9. df2 <- tribble(
  10. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  11. 'Mi', 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  12. 'Mi', 28, 1019, 289, 277, 78, 2, 2, 1, 3
  13. )
  14. dfs1 <- c('df1','df2')
  15. var <- c('City_Name', 'Temp', 'Pres' , 'Wind_Hor' , 'Wind_Ver' , 'Rainf' , 'S_Moist')
  16. # 使用mget一次获取所有数据集
  17. df_list <- mget(dfs1, envir = .GlobalEnv, inherits = TRUE)
  18. lapply(df_list, \(x) {
  19. icol <- var[!var %in% names(x)]
  20. x[icol] <- NA
  21. x[var]
  22. }) ->
  23. list2env(envir = .GlobalEnv)
  24. # <environment: R_GlobalEnv>
  25. df1
  26. # # A tibble: 2 × 7
  27. # City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
  28. # <chr> <dbl> <dbl> <dbl> <dbl> <lgl> <dbl>
  29. # 1 Mi 27 1019 287 278 NA 78
  30. # 2 Mi 28 1019 289 277 NA 78
  31. df2
  32. # # A tibble: 2 × 7
  33. # City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
  34. # <chr> <dbl> <dbl> <dbl> <lgl> <dbl> <dbl>
  35. # 1 Mi 27 1019 287 NA 278 78
  36. # 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).

  1. suppressPackageStartupMessages(
  2. library(tibble)
  3. )
  4. df1 &lt;- tribble(
  5. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Wind_Ver, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  6. &#39;Mi&#39;, 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  7. &#39;Mi&#39;, 28, 1019, 289, 277, 78, 2, 2, 1, 3
  8. )
  9. df2 &lt;- tribble(
  10. ~City_Name, ~Temp, ~Pres, ~Wind_Hor, ~Rainf, ~S_Moist, ~Temp1, ~Pres1, ~Wind1, ~S_Moist1,
  11. &#39;Mi&#39;, 27, 1019, 287, 278, 78, 1, 2, 2, 1,
  12. &#39;Mi&#39;, 28, 1019, 289, 277, 78, 2, 2, 1, 3
  13. )
  14. dfs1 &lt;- c(&#39;df1&#39;,&#39;df2&#39;)
  15. 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;)
  16. # get all data sets in one call to mget
  17. df_list &lt;- mget(dfs1, envir = .GlobalEnv, inherits = TRUE)
  18. lapply(df_list, \(x) {
  19. icol &lt;- var[!var %in% names(x)]
  20. x[icol] &lt;- NA
  21. x[var]
  22. }) |&gt;
  23. list2env(envir = .GlobalEnv)
  24. #&gt; &lt;environment: R_GlobalEnv&gt;
  25. df1
  26. #&gt; # A tibble: 2 &#215; 7
  27. #&gt; City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
  28. #&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dbl&gt;
  29. #&gt; 1 Mi 27 1019 287 278 NA 78
  30. #&gt; 2 Mi 28 1019 289 277 NA 78
  31. df2
  32. #&gt; # A tibble: 2 &#215; 7
  33. #&gt; City_Name Temp Pres Wind_Hor Wind_Ver Rainf S_Moist
  34. #&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dbl&gt; &lt;dbl&gt;
  35. #&gt; 1 Mi 27 1019 287 NA 278 78
  36. #&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:

确定