R:使用get()

时间:2018-02-08 02:33:52

标签: r function

我试图得到一系列由两个变量分组的记录总和:列'a',然后是我的数据集中的其余列。

当我在下面运行时:

vars <- c(colnames(df))
vars<-vars[-1]

counting<-function(index) {
  count(df,a,get(index))
}

vars[]<-lapply(vars,FUN=counting)

get(index)生成的列的名称在我的列表变量中命名为“get(index)”。如何更改此设置以使输出中的列名与原始列名相同?

例如,如果我有这个数据帧,(这个数据框是从datacamp中的教程修改的):

First.Name <- c("John", "Edgar", "Walt", "Jane")
Second.Name <- c("Doe", "Poe", "Whitman", "Austen")
Sex <- c("MALE", "MALE", "MALE", "FEMALE")
writers_df <- data.frame(First.Name, Second.Name, Sex)

我想计算有多少行具有Sex和其他变量的唯一组合,我会运行:

vars <- c(colnames(writers_df))
vars<-vars[-3]
counting<-function(index) {
  count(df,Sex,get(index))
}

vars[]<-lapply(vars,FUN=counting)

其中一个表的输出如下:

Sex get(index) n
M   John       1
M   Edgar      1
M   Walt       1
F   Jane       1

如何在不手动更改“手动”的情况下获取名为First.Name的列(索引)?

2 个答案:

答案 0 :(得分:2)

使用下划线版本:count_

counting <- function(index) {
  count_(writers_df, c('Sex', index))
}

> lapply(vars,FUN=counting)
[[1]]
# A tibble: 4 x 3
  Sex    First.Name     n
  <fct>  <fct>      <int>
1 FEMALE Jane           1
2 MALE   Edgar          1
3 MALE   John           1
4 MALE   Walt           1

[[2]]
# A tibble: 4 x 3
  Sex    Second.Name     n
  <fct>  <fct>       <int>
1 FEMALE Austen          1
2 MALE   Doe             1
3 MALE   Poe             1
4 MALE   Whitman         1

[[3]]
# A tibble: 2 x 2
  Sex        n
  <fct>  <int>
1 FEMALE     1
2 MALE       3

答案 1 :(得分:1)

试试这个:

counting<-function(var) {
  count(writers_df,Sex,!!rlang::sym(var))
}

> lapply(vars,counting)

[[1]]
# A tibble: 4 x 3
     Sex First.Name     n
  <fctr>     <fctr> <int>
1 FEMALE       Jane     1
2   MALE      Edgar     1
3   MALE       John     1
4   MALE       Walt     1

[[2]]
# A tibble: 4 x 3
     Sex Second.Name     n
  <fctr>      <fctr> <int>
1 FEMALE      Austen     1
2   MALE         Doe     1
3   MALE         Poe     1
4   MALE     Whitman     1