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

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

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)

  1. makeMean <- function(df, colName){
  2. x <- c()
  3. levels <- levels(beetleData$Herbicide.dosage)
  4. for(i in levels){
  5. tempdata <- summarize(df, mean.temp = mean(Species.richness[Herbicide.dosage == i]))
  6. x <- append(x, tempdata)
  7. }
  8. x <- unlist(x)
  9. return(x)
  10. }

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)

  1. makeMean &lt;- function(df, colName){
  2. x &lt;- c()
  3. levels &lt;- levels(beetleData$Herbicide.dosage)
  4. for(i in levels){
  5. tempdata &lt;- summarize(df, mean.temp = mean(Species.richness[Herbicide.dosage == i]))
  6. x &lt;- append(x, tempdata)
  7. }
  8. x &lt;- unlist(x)
  9. return(x)
  10. }

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编程概念会有所帮助。链接

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

  1. library(dplyr)
  2. makeMeans <- function(df, colName) {
  3. df %>%
  4. summarise(Mean = mean({{colName}}, na.rm = TRUE))
  5. }
  6. makeMeans(mtcars, mpg)

返回结果如下:

  1. Mean
  2. 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 .

  1. library(dplyr)
  2. makeMeans &lt;- function(df,colName){
  3. df%&gt;%
  4. summarise(Mean= mean({{colName}},na.rm=T))
  5. }
  6. makeMeans(mtcars,mpg)

returns

  1. Mean
  2. 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 的回答,但是传递一个字符串。"

  1. library(tidyverse)
  2. makeMeans <- function(df, colName){
  3. df %>%
  4. summarise(Mean = mean(.data[[colName]], na.rm = TRUE))
  5. }

"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.

  1. library(tidyverse)
  2. makeMeans &lt;- function(df, colName){
  3. df %&gt;%
  4. summarise(Mean = mean(.data[[colName]],na.rm=T))
  5. }

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

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:

确定