我经常有一个来自某些计算的数据框,我想在输出之前对其进行清理、重命名和列排列。以下所有版本都可以使用,最简单的data.frame
是最接近的。
有没有办法将 和 的数据帧内计算within
与mutate
的列顺序保存相结合data.frame()
,而最后没有额外的和冗余的 [,....]?
library(plyr)
# Given this chaotically named data.frame
d = expand.grid(VISIT=as.factor(1:2),Biochem=letters[1:2],time=1:5,
subj=as.factor(1:3))
d$Value1 =round(rnorm(nrow(d)),2)
d$val2 = round(rnorm(nrow(d)),2)
# I would like to cleanup, compute and rearrange columns
# Simple and almost perfect
dDataframe = with(d, data.frame(
biochem = Biochem,
subj = subj,
visit = VISIT,
value1 = Value1*3
))
# This simple solution is almost perfect,
# but requires one more line
dDataframe$value2 = dDataframe$value1*d$val2
# For the following methods I have to reorder
# and select in a second step
# use mutate from plyr to allow computation on computed values,
# which transform cannot do.
dMutate = mutate(d,
biochem = Biochem,
subj = subj,
visit = VISIT,
value1 = Value1*3, #assume this is a time consuming function
value2 = value1*val2
# Could set fields = NULL here to remove,
# but this does not help getting column order
)[,c("biochem","subj","visit","value1","value2")]
# use within. Same problem, order not preserved
dWithin = within(d, {
biochem = Biochem
subj = subj
visit = VISIT
value1 = Value1*3
value2 = value1*val2
})[,c("biochem","subj","visit","value1","value2")]
all.equal(dDataframe,dWithin)
all.equal(dDataframe,dMutate)