我不知道为什么会出错
Error: object 'grade' not found
组成函数时。
等级显然在数据集中,并包含在函数中。如果我不使用该功能,而只是使用
dat%>%
cohort.fun()%>%
group_by(cohort, variable, timepoint)%>%
summarize(perf_measure = mean(measure))
一切正常。这是我正在使用的两个功能:
library(reshape2)
library(tidyverse)
cohort.fun <- function(dat){
dat%>%
mutate(grade = as.numeric(grade))%>%
mutate(cohort = ifelse(grade%in%c(3,4),3,ifelse(
grade%in%c(5,6), 5, ifelse(
grade%in%c(7,8), 7, grade))))%>%
mutate(cohort = as.character(cohort))
}
melt.fun <- function(dat){
melt(c("pid", "grade", "timepoint"), value.name = "measure")%>%
cohort.fun()
}
然后我跑步
dat%>%
melt.fun()
,我得到上面的错误。有任何想法吗?非常感谢!
这是赔率:
structure(list(pid = c("ADMIN-UCSF-bo004", "ADMIN-UCSF-bo005",
"ADMIN-UCSF-bo008", "ADMIN-UCSF-bo010", "ADMIN-UCSF-bo011", "ADMIN-UCSF-bo012",
"ADMIN-UCSF-bo013", "ADMIN-UCSF-bo014", "ADMIN-UCSF-bo015", "ADMIN-UCSF-bo016"
), grade = c("3", "3", "3", "3", "3", "3", "3", "3", "3", "3"
), RC1 = c(-1.81295211570392, -1.31252376878321, -1.1701654183369,
-1.58244557144815, -1.95383829351231, -0.516109923323212, -0.370765686983851,
-1.93212644807752, -1.6241046548069, -1.34160382084709), RC2 = c(-0.363819589341912,
0.268206917949323, -2.24123725035034, -0.25274997192688, 0.313608190056975,
-0.0393486670413662, -0.0623610937831014, 0.803692668734253,
0.416065992573585, -0.069880541013785), RC3 = c(-2.69157047028032,
-0.822917456389246, -1.52186068360016, -0.590070546800741, 0.583790188582597,
-0.253888391947117, 1.22197349838073, -1.63335701437031, 1.24595192142446,
0.0191275904777839), timepoint = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-10L))
答案 0 :(得分:2)
在函数melt.fun
中,您实际传递给cohort.fun
的是melt
而不是dat
的结果。 Ergo cohort.fun
找不到其参数。
定义melt.fun
如下:
melt.fun <- function(dat){
melt(dat, c("pid", "grade", "timepoint"), value.name = "measure") %>%
cohort.fun()
}
应该做到这一点。您可以看到magrittr的前向管道运算符here
的使用编辑:我将整个脚本包括在这里,以便大家看到到目前为止我所做的事情:
#Loading libraries
library(tidyverse)
library(reshape2)
#Loading data
dat <- structure(list(pid = c("ADMIN-UCSF-bo004", "ADMIN-UCSF-bo005",
"ADMIN-UCSF-bo008", "ADMIN-UCSF-bo010", "ADMIN-UCSF-bo011", "ADMIN-UCSF-bo012", "ADMIN-UCSF-bo013", "ADMIN-UCSF-bo014", "ADMIN-UCSF-bo015", "ADMIN-UCSF-bo016"), grade = c("3", "3", "3", "3", "3", "3", "3", "3", "3", "3"), RC1 = c(-1.81295211570392, -1.31252376878321, -1.1701654183369, -1.58244557144815, -1.95383829351231, -0.516109923323212, -0.370765686983851, -1.93212644807752, -1.6241046548069, -1.34160382084709), RC2 = c(-0.363819589341912, 0.268206917949323, -2.24123725035034, -0.25274997192688, 0.313608190056975, -0.0393486670413662, -0.0623610937831014, 0.803692668734253, 0.416065992573585, -0.069880541013785), RC3 = c(-2.69157047028032, -0.822917456389246, -1.52186068360016, -0.590070546800741, 0.583790188582597, -0.253888391947117, 1.22197349838073, -1.63335701437031, 1.24595192142446, 0.0191275904777839), timepoint = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L))
#Defining functions
#Function1
cohort.fun <- function(dat){
dat%>%
mutate(grade = as.numeric(grade))%>%
mutate(cohort = ifelse(grade%in%c(3,4),3,ifelse(
grade%in%c(5,6), 5, ifelse(
grade%in%c(7,8), 7, grade))))%>%
mutate(cohort = as.character(cohort))
}
#Function2
melt.fun <- function(dat){
melt(dat, c("pid", "grade", "timepoint"), value.name = "measure") %>%
cohort.fun()
}
#Executing
dat%>%
melt.fun()
#Result
> dat%>%
+ melt.fun()
pid grade timepoint variable measure cohort
1 ADMIN-UCSF-bo004 3 1 RC1 -1.81295212 3
2 ADMIN-UCSF-bo005 3 1 RC1 -1.31252377 3
3 ADMIN-UCSF-bo008 3 1 RC1 -1.17016542 3
4 ADMIN-UCSF-bo010 3 1 RC1 -1.58244557 3
5 ADMIN-UCSF-bo011 3 1 RC1 -1.95383829 3
6 ADMIN-UCSF-bo012 3 1 RC1 -0.51610992 3
7 ADMIN-UCSF-bo013 3 1 RC1 -0.37076569 3
8 ADMIN-UCSF-bo014 3 1 RC1 -1.93212645 3
9 ADMIN-UCSF-bo015 3 1 RC1 -1.62410465 3
10 ADMIN-UCSF-bo016 3 1 RC1 -1.34160382 3
11 ADMIN-UCSF-bo004 3 1 RC2 -0.36381959 3
12 ADMIN-UCSF-bo005 3 1 RC2 0.26820692 3
13 ADMIN-UCSF-bo008 3 1 RC2 -2.24123725 3
14 ADMIN-UCSF-bo010 3 1 RC2 -0.25274997 3
15 ADMIN-UCSF-bo011 3 1 RC2 0.31360819 3
16 ADMIN-UCSF-bo012 3 1 RC2 -0.03934867 3
17 ADMIN-UCSF-bo013 3 1 RC2 -0.06236109 3
18 ADMIN-UCSF-bo014 3 1 RC2 0.80369267 3
19 ADMIN-UCSF-bo015 3 1 RC2 0.41606599 3
20 ADMIN-UCSF-bo016 3 1 RC2 -0.06988054 3
21 ADMIN-UCSF-bo004 3 1 RC3 -2.69157047 3
22 ADMIN-UCSF-bo005 3 1 RC3 -0.82291746 3
23 ADMIN-UCSF-bo008 3 1 RC3 -1.52186068 3
24 ADMIN-UCSF-bo010 3 1 RC3 -0.59007055 3
25 ADMIN-UCSF-bo011 3 1 RC3 0.58379019 3
26 ADMIN-UCSF-bo012 3 1 RC3 -0.25388839 3
27 ADMIN-UCSF-bo013 3 1 RC3 1.22197350 3
28 ADMIN-UCSF-bo014 3 1 RC3 -1.63335701 3
29 ADMIN-UCSF-bo015 3 1 RC3 1.24595192 3
30 ADMIN-UCSF-bo016 3 1 RC3 0.01912759 3