我有一个数据集,其中的每一行都包含Combinevar函数所需的数据(package = fishmethods; combinvar组合来自两个分布的信息以得出组合方差)。
xbar1 = c(2,2,1,4,3)
xbar2 = c(0,0,0,0,0)
var1 = c(0,1,3,2,1)
var2 = c(0,0,0,0,0)
n1 = c(50,10,30,40,50)
n2 = c(3,4,50,32,20)
df <- data.frame(xbar1, xbar2, var1, var2, n1, n2)
xbar1 xbar2 var1 var2 n1 n2
2 0 0 0 50 3
2 0 1 0 10 4
1 0 3 0 30 50
4 0 2 0 40 32
3 0 1 0 50 20
我将如何在行之间应用该函数。我可以在这样的for循环中做到这一点:
for (i in 1:nrow(df)) {
combined_var <- combinevar(xbar = c(df$xbar1[i], df$xbar2[i]),
s_squared = c(df$var1[i], df$var2[i]),
n = c(df$n1[i], df$n2[i]))[2]
print(combined_var)
}
[1] 0.2177068
[1] 1.571429
[1] 1.338608
[1] 5.104851
[1] 2.573499
但是我敢肯定有更好的方法。我想我可以使用apply函数来做到这一点,但我不知道怎么做。
答案 0 :(得分:2)
您可以对行使用apply函数,并正确指定要读取行的函数:
x='10 0 17 5'
a = x.split(' ')
first = a.pop(0)
print(first)
print(a)
c = a.sort(key=int)
print(c)
d = sorted(a)
print(d)
e = a.sort()
print(e)
f = a.reverse()
print(f)
//
10
['0', '17', '5']
None
['0', '17', '5']
None
None
答案 1 :(得分:1)
我们可以按行嵌套数据,然后为每行映射函数。
library(tidyverse)
library(fishmethods)
df %>%
rownames_to_column("row") %>%
nest(-row) %>%
mutate(combined_var = map(data, ~combinevar(xbar = c(.x$xbar1, .x$xbar2),
s_squared = c(.x$var1, .x$var2),
n = c(.x$n1, .x$n2))[2])) %>%
unnest()
#> row combined_var xbar1 xbar2 var1 var2 n1 n2
#> 1 1 0.2177068 2 0 0 0 50 3
#> 2 2 1.5714286 2 0 1 0 10 4
#> 3 3 1.3386076 1 0 3 0 30 50
#> 4 4 5.1048513 4 0 2 0 40 32
#> 5 5 2.5734990 3 0 1 0 50 20
或者我们可以按行应用该函数
df %>%
rowwise() %>%
mutate(combined_var = combinevar(xbar = c(xbar1, xbar2),
s_squared = c(var1, var2),
n = c(n1, n2))[2])
#> Source: local data frame [5 x 7]
#> Groups: <by row>
#>
#> # A tibble: 5 x 7
#> xbar1 xbar2 var1 var2 n1 n2 combined_var
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2 0 0 0 50 3 0.218
#> 2 2 0 1 0 10 4 1.57
#> 3 1 0 3 0 30 50 1.34
#> 4 4 0 2 0 40 32 5.10
#> 5 3 0 1 0 50 20 2.57
由reprex package(v0.2.0)于2018-08-19创建。