我有一个包含偏斜数据的数据集。
d = data.frame(x = sample(1:15, size = 100, replace = TRUE, prob = 1:15),
y = sample(1:20, size = 50, replace = TRUE, prob = 20:1))
我想对每列应用Johnson变换。我尝试使用apply函数。
library(jtrans)
x = apply(d,2,jtrans)
但是,由于转换后的值在列表中(即x $ x $ transformed或x $ y $ transformed),我不知道如何将其应用于数据框?
答案 0 :(得分:0)
您可以定义一个自定义函数并从结果对象中提取转换后的值。
library(jtrans)
#> Warning: package 'jtrans' was built under R version 3.4.4
set.seed(1)
d = data.frame(x = sample(1:15, size = 100, replace = TRUE, prob = 1:15),
y = sample(1:20, size = 50, replace = TRUE, prob = 20:1))
new_d <- sapply(d, function(x) jtrans(x)$transformed)
head(new_d)
#> x y
#> [1,] 0.8277242 0.55000000
#> [2,] 0.4400000 -0.06683963
#> [3,] -0.1764075 -0.55000000
#> [4,] -1.3760105 1.79402536
#> [5,] 1.3200000 0.55000000
#> [6,] -1.3760105 -0.55000000
由reprex package(v0.2.1)于2019-06-03创建