【发布时间】:2019-06-03 16:17:16
【问题描述】:
我有样本运行并生成了 8 个变量的数据。每个样本都有一个“NIL”运行,以及同一样本的 3 个其他“版本”(有时是 4 个或 5 个)。我需要的是,创建 8 个新变量,这将是每个版本的样本与该样本的 NIL 的倍数变化。
要对所有变量运行它,我知道我需要使用 mutate_at(.funs=funs(FUNCTION HERE), .vars=var1:var8) 之类的东西。但是,我无法弄清楚要使用什么功能。
x<-c(50,25,10,5)
y<-c(100,50,25,10)
z<-c(50,25,10,5)
sample<-c("a","a","a","a")
sample_type<-c("a_bcg","a_esf","a_hiv","a_nil")
## sample_type column is the one which would serve as a reference for
## me of how these "a"s are different from each other but for
## calculation purposes we can just group by sample
test<-as.tibble(cbind(x,y,z,sample))
test<-test%>%
modify_at(c(1,2,3),as.numeric)
test%>%
group_by(sample)%>% ## since there'd be more groups
mutate_at(.funs = funs(./slice(n)),.vars = (1:3))
这失败了 mutate_impl(.data, dots) 中的错误: 评估错误:没有适用于 'slice_' 的方法应用于 “c('double', 'numeric')”类的对象。
我需要创建一个函数,该函数获取一列的每一行的值并将其与特定行(在该子组中)相除 - 该行中有一个“nil”,因此我可以使它nil 样本可能始终是组中的第一个或最后一个。
The expected would look like this
x y z sample x_1 y_1 z_1
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 50 100 50 a 10 10 10
2 25 50 25 b 5 5 5
3 10 25 10 c 2 2.5 2
4 5 10 5 nil 1 1 1
【问题讨论】: