apply() 和 adply() 的不同输出
Different output from apply() and adply()
我不明白为什么 adply()
return 给我一个包含原始变量的 data.frame
,而 apply()
没有。基本上,看起来 adply()
只是另一个 mutate()
。我错过了什么?
测试数据:
library(pacman)
p_load(plyr)
g_loadings = c(.50, .60, .80,
.60, .70, .60,
.80, .50, .30)
group_1_loadings = c(.50, .50, .50,
0, 0, 0,
0, 0, 0)
group_2_loadings = c(0, 0, 0,
.50, .50, .50,
0, 0, 0)
group_3_loadings = c(0, 0, 0,
0, 0, 0,
.50, .50, .50)
d = data.frame(g_loadings,
group_1_loadings,
group_2_loadings,
group_3_loadings)
adply()
:
adply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}
)
Returns:
g_loadings group_1_loadings group_2_loadings group_3_loadings V1
1 0.5 0.5 0.0 0.0 0.7071068
2 0.6 0.5 0.0 0.0 0.6244998
3 0.8 0.5 0.0 0.0 0.3316625
4 0.6 0.0 0.5 0.0 0.6244998
5 0.7 0.0 0.5 0.0 0.5099020
6 0.6 0.0 0.5 0.0 0.6244998
7 0.8 0.0 0.0 0.5 0.3316625
8 0.5 0.0 0.0 0.5 0.7071068
9 0.3 0.0 0.0 0.5 0.8124038
apply()
:
apply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}
)
Returns:
[1] 0.7071068 0.6244998 0.3316625 0.6244998 0.5099020 0.6244998 0.3316625 0.7071068 0.8124038
为什么 adply()
return 和 apply()
不一样?
那些plyr
函数的命名约定是第一个字母对应它操作的数据结构,第二个字母对应它return的数据结构。因此,adply
对数组和 returns data.frame
进行操作。您可以使用 .expand
选项将列指定为 return.
adply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}, .expand=F)
# X1 V1
# 1 1 0.7071068
# 2 2 0.6244998
# 3 3 0.3316625
# 4 4 0.6244998
# 5 5 0.5099020
# 6 6 0.6244998
# 7 7 0.3316625
# 8 8 0.7071068
# 9 9 0.8124038
或者用 aaply
返回一个数组(这与 apply
编辑的 return 相同,除了 apply
使用 as.vector
在结果上)
aaply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}, .expand=F)
# 1 2 3 4 5 6 7 8
# 0.7071068 0.6244998 0.3316625 0.6244998 0.5099020 0.6244998 0.3316625 0.7071068
# 9
# 0.8124038
我不明白为什么 adply()
return 给我一个包含原始变量的 data.frame
,而 apply()
没有。基本上,看起来 adply()
只是另一个 mutate()
。我错过了什么?
测试数据:
library(pacman)
p_load(plyr)
g_loadings = c(.50, .60, .80,
.60, .70, .60,
.80, .50, .30)
group_1_loadings = c(.50, .50, .50,
0, 0, 0,
0, 0, 0)
group_2_loadings = c(0, 0, 0,
.50, .50, .50,
0, 0, 0)
group_3_loadings = c(0, 0, 0,
0, 0, 0,
.50, .50, .50)
d = data.frame(g_loadings,
group_1_loadings,
group_2_loadings,
group_3_loadings)
adply()
:
adply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}
)
Returns:
g_loadings group_1_loadings group_2_loadings group_3_loadings V1
1 0.5 0.5 0.0 0.0 0.7071068
2 0.6 0.5 0.0 0.0 0.6244998
3 0.8 0.5 0.0 0.0 0.3316625
4 0.6 0.0 0.5 0.0 0.6244998
5 0.7 0.0 0.5 0.0 0.5099020
6 0.6 0.0 0.5 0.0 0.6244998
7 0.8 0.0 0.0 0.5 0.3316625
8 0.5 0.0 0.0 0.5 0.7071068
9 0.3 0.0 0.0 0.5 0.8124038
apply()
:
apply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}
)
Returns:
[1] 0.7071068 0.6244998 0.3316625 0.6244998 0.5099020 0.6244998 0.3316625 0.7071068 0.8124038
为什么 adply()
return 和 apply()
不一样?
那些plyr
函数的命名约定是第一个字母对应它操作的数据结构,第二个字母对应它return的数据结构。因此,adply
对数组和 returns data.frame
进行操作。您可以使用 .expand
选项将列指定为 return.
adply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}, .expand=F)
# X1 V1
# 1 1 0.7071068
# 2 2 0.6244998
# 3 3 0.3316625
# 4 4 0.6244998
# 5 5 0.5099020
# 6 6 0.6244998
# 7 7 0.3316625
# 8 8 0.7071068
# 9 9 0.8124038
或者用 aaply
返回一个数组(这与 apply
编辑的 return 相同,除了 apply
使用 as.vector
在结果上)
aaply(d, 1, function(x) {
var_g_group = sum(x^2)
var_remain = 1 - var_g_group
loading_specificity = sqrt(var_remain)
return(loading_specificity)
}, .expand=F)
# 1 2 3 4 5 6 7 8
# 0.7071068 0.6244998 0.3316625 0.6244998 0.5099020 0.6244998 0.3316625 0.7071068
# 9
# 0.8124038