将字符串作为数据列参数传递(同时具有另一个列标识符)。

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

Passing a string as argument for data column (whilst having another column identifier)

问题

I'm writing a function that finds the mean at 4 different categorical variables and returns a size 4 vector with all of these values. In doing so, I want to pass different columns of a dataframe such that I can find these 4 values for any column of the dataframe. (I would like for the dataframe to be variable as well but thats not important)

makeMean <- function(df, colName){
  x <- c()
  levels <- levels(beetleData$Herbicide.dosage)
  for(i in levels){
    tempdata <- summarize(df, mean.temp = mean(Species.richness[Herbicide.dosage == i]))
    x <- append(x, tempdata)
  }
  x <- unlist(x) 
  return(x)
}

This is the function. Species.richness is the column name I'd like to replace with "colName" but I can't find a way to do that. I can pass it by doing df[colName] (I think) but I'd like to have the other filter too (Herbicide.dosage == i).

R doesn't let me do mean(colName[Herbicide.dosage == i]). I've tried a couple other ways as well and none work as R doesn't convert the string to the dataframe column I want, it just reads it as a string.

英文:

I'm writing a function that finds the mean at 4 different categorical variables and returns a size 4 vector with all of these values. In doing so, I want to pass different columns of a dataframe such that I can find these 4 values for any column of the dataframe. (I would like for the dataframe to be variable as well but thats not important)

makeMean &lt;- function(df, colName){
  x &lt;- c()
  levels &lt;- levels(beetleData$Herbicide.dosage)
  for(i in levels){
    tempdata &lt;- summarize(df, mean.temp = mean(Species.richness[Herbicide.dosage == i]))
    x &lt;- append(x, tempdata)
  }
  x &lt;- unlist(x) 
  return(x)
}

This is the function. Species.richness is the column name I'd like to replace with "colName" but I can't find a way to do that. I can pass it by doing df[colName] (I think) but I'd like to have the other filter too (Herbicide.dosage == i).

R doesn't let me do mean(colName[Herbicide.dosage == i]). I've tried a couple other ways as well and none work as R doesn't convert the string to the dataframe column I want, it just reads it as a string

答案1

得分: 1

将列名传递给函数有点棘手。但dplyr编程概念会有所帮助。链接

无法在您的数据上工作,但可以通过以下方式传递一个不是字符串的列名。

library(dplyr)
makeMeans <- function(df, colName) {
  df %>%
    summarise(Mean = mean({{colName}}, na.rm = TRUE))
}

makeMeans(mtcars, mpg)

返回结果如下:

      Mean
1 20.09062

这样,您可以使用双花括号{{}}在函数中传递列并进行管理。

可能还有其他方法,但以这种方式传递列很容易。您可以尝试链接中提到的方法。

英文:

Passing column names to a function is tricky. But dplyr programming concepts will help. Link

Cannot work on ur data but passing a column name which is not read a sring is possible this way .

library(dplyr)
makeMeans &lt;- function(df,colName){
  
 df%&gt;%
    summarise(Mean=    mean({{colName}},na.rm=T))
  
}


makeMeans(mtcars,mpg)

returns

      Mean
1 20.09062

This way you can just pass columns and manage them in your function with double curly brackets {{

there might be other ways but passing columns this way is easy. You can try approaches in the link

答案2

得分: 1

"Like @anuanand's answer, but passing a string."

"类似于 @anuanand 的回答,但是传递一个字符串。"

library(tidyverse)

makeMeans <- function(df, colName){
  
  df %>%
    summarise(Mean = mean(.data[[colName]], na.rm = TRUE))
}

"See: https://www.tidyverse.org/blog/2019/06/rlang-0-4-0/ regarding passing column names as part of non-standard evaluation (NSE) in R."

"参考:https://www.tidyverse.org/blog/2019/06/rlang-0-4-0/,了解如何在 R 中作为非标准评估(NSE)的一部分传递列名。"

英文:

Like @anuanand's answer, but passing a string.

library(tidyverse)

makeMeans &lt;- function(df, colName){
  
  df %&gt;%
    summarise(Mean = mean(.data[[colName]],na.rm=T))
}

将字符串作为数据列参数传递(同时具有另一个列标识符)。

See: https://www.tidyverse.org/blog/2019/06/rlang-0-4-0/ regarding passing column names as part of non-standard evaulation (NSE) in R.

huangapple
  • 本文由 发表于 2023年3月7日 09:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657414.html
匿名

发表评论

匿名网友

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

确定