データフレームは、ベクトルではなく行列です。私がしたいRでは、データフレームのベクトル値の列を複数の列に分割します。
> dim(results)
[1] 2 2
> results[,2]
[,1] [,2] [,3]
[1,] 0.4710224 0.4280053 0.3206661
[2,] 0.5769064 0.6220120 0.2683387
:たとえば、この
set.seed(101)
Df <- data.frame(x = runif(100),
y = round(runif(100)))
descriptives <- function(arg) c(mean(arg), median(arg), sd(arg))
results <- aggregate(x ~ y, data=Df, descriptives)
は私に2番目の列は2×3行列であることと、2×2のデータフレームである
> results
y x.1 x.2 x.3
1 0 0.4710224 0.4280053 0.3206661
2 1 0.5769064 0.6220120 0.2683387
を与える
その行列を3つの列ベクトルに分割します。
私は
results <- cbind(results$y, as.data.frame(results[,2]))
names(results) <- c('y', 'mean', 'median', 'sd')
> results
y mean median sd
1 0 0.4710224 0.4280053 0.3206661
2 1 0.5769064 0.6220120 0.2683387
を行うことができます。しかし、より直接的かつ少ないハック方法はありますか?私はtidyr
のseparate
のようなものを考えていますか?この場合
'as.data.frame(マトリックス(非公開に(結果)、nrow = nrow(結果)))'のようなもの? – Sotos