简体   繁体   English

使用dplyr :: mutate创建一个新变量,并粘贴两个现有变量以供用户定义函数

[英]Create a new variable using dplyr::mutate and pasting two existing variables for user-defined function

I would like to create a function to join the lower and higher bound of confidence intervals (named as CIlow and CIhigh ) from a data frame. 我想创建一个函数来连接数据帧中置信区间的下限和上限(命名为CIlowCIhigh )。 See the data frame below as example. 请参见下面的数据框作为示例。

dataframe<-data.frame(CIlow_a=c(1.1,1.2),CIlow_b=c(2.1,2.2),CIlow_c=c(3.1,3.2),
                      CIhigh_a=c(1.3,1.4),CIhigh_b=c(2.3,2.4),CIhigh_c=c(3.3,3.4))

The data frame has CIlow and CIhigh for a number of groups (named as a , b and c ) and for a number of variables (in this case two, the rows of the data frame). 对于多个组(称为abc )以及多个变量(在这种情况下为数据帧的行),数据帧具有CIlowCIhigh

group<-c("a","b","c")

To build my own function I tried the following code: 为了构建自己的功能,我尝试了以下代码:

f<-function(df,gr){

enquo_df<-enquo(df)
enquo_gr<-enquo(gr)

r<-df%>%
   dplyr::mutate(UQ(paste("CI",enquo_gr,sep="_")):=
                   sprintf("(%s,%s)",
                           paste("CIlow",quo_name(enquo_gr),sep="_"),
                           paste("CIhigh",quo_name(enquo_gr),sep="_")))

return(r)
}

However when using the function 但是使用该功能时

library(dplyr)

group<-c("a","b","c")
    dataframe<-data.frame(CIlow_a=c(1.1,1.2),CIlow_b=c(2.1,2.2),CIlow_c=c(3.1,3.2),CIhigh_a=c(1.3,1.4),CIhigh_b=c(2.3,2.4),CIhigh_c=c(3.3,3.4))

f(df=dataframe,gr=group)

I do not get the expected output 我没有得到预期的输出

output<-data.frame(CI_a=c("(1.1,1.3)","(1.2,1.4)"),
                  CI_b=c("(2.1,2.3)","(2.2,2.4)"),
                  CI_c=c("(3.1,3.3)","(3.2,3.4)"))

but the following error message: 但是出现以下错误信息:

Error: LHS must be a name or string 错误:LHS必须是名称或字符串

Do you know why? 你知道为什么吗? How could I solve this issue? 我该如何解决这个问题? Thanks in advance. 提前致谢。

Old school solution: 老派解决方案:

res <- as.data.frame(matrix(NA_character_, nrow(dataframe), ncol(dataframe) / 2))
for (i in seq_along(group)) {
  var <- paste0("CI", c("low", "high"), "_", group[[i]])
  res[[i]] <- sprintf("(%s,%s)", dataframe[[var[[1]]]], dataframe[[var[[2]]]])
}
names(res) <- paste0("CI_", group)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建(dplyr :: mutate)并选择(dplyr :: select)新变量,这些变量通过用户定义的函数粘贴两个现有列 - Create (dplyr::mutate) and select (dplyr::select) new variables that paste two existing columns by means of a user-defined function 具有dplyr的用户定义函数-根据组合参数对列进行突变 - User-defined function with dplyr - mutate columns based on combining arguments 用户定义的 function 和 dplyr - 变异列是一个参数 - User-defined function with dplyr - mutate column is a argument 通过粘贴两个因子变量来创建一个新变量 - Create a new variable by pasting two factor variables 使用dplyr在用户定义函数中进行子集 - Subsetting within user-defined function using dplyr 在用户定义的函数中使用Dplyr汇总数据,然后绘制数据 - Using Dplyr within a user-defined function to summarise data then plot it 使用 dplyr 变异 function 根据当前行有条件地创建新变量 - Using dplyr mutate function to create new variable conditionally based on current row dplyr mutate_at:使用重新编码创建新变量 - dplyr mutate_at: create new variables with recode 如何使用 Mutate function 创建新变量? - How to create a new variable using the Mutate function? dplyr:创建一个新变量作为所有现有变量的函数,而不定义它们的名称 - dplyr: Create a new variable as a function of all existing variables without defining their names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM