[英]Applying function to every row using a range of columns (R)
My data contains consecutive columns V1-V1998 with other columns at either side of these. 我的数据包含连续的列V1-V1998,而在这些列的任一侧都有其他列。 I want to calculate the skewness of the rows within this range of 1998 columns.
我想计算此1998列范围内的行的偏度。
Here is the code I tried: 这是我尝试的代码:
ND2a <- NoDup2 %>%
rowwise() %>%
mutate(skew2 = skewness(V1:V1998))
This creates a new column called skew2 however the skewness isn't calculated and instead the column is filled with "NaN". 这将创建一个名为skew2的新列,但是不会计算偏斜度,而是用“ NaN”填充该列。 Does anyone know why this might be?
有谁知道为什么会这样吗?
I'm using skewness from the moments package. 我正在使用“时刻”包中的偏度。
My data looks a little like this 我的数据看起来像这样
Data V1 V2 V3 ..... V1998 ....
Acaricomes phytoseiuli 0.01 0.0 0.002 0.03
Acetivibrio cellulolyticus 0.005 0.002 0.011 0.04
Acetobacter aceti 0.001 0.003 0.004 0.0
You can do: 你可以做:
library(e1071)
# get column names
cols <- paste0('V', seq(1,1998,1))
# apply function on selected columns
NoDup2$skew_value <- apply(NoDup2[,cols], 1, skewness)
With this we calculate skewness for every row across all columns in the given data set. 这样,我们就可以计算给定数据集中所有列的每一行的偏斜度。
I would try, but depends on what you want to do afterwards. 我会尝试,但要取决于您以后要做什么。
library(tidyverse)
iris %>%
gather(key, value, -Species) %>%
group_by(Species) %>%
mutate(skew2=moments::skewness(value)) %>%
slice(1:2)
# A tibble: 6 x 4
# Groups: Species [3]
Species key value skew2
<fct> <chr> <dbl> <dbl>
1 setosa Sepal.Length 5.10 0.146
2 setosa Sepal.Length 4.90 0.146
3 versicolor Sepal.Length 7.00 0.157
4 versicolor Sepal.Length 6.40 0.157
5 virginica Sepal.Length 6.30 0.128
6 virginica Sepal.Length 5.80 0.128
I used the iris
data as it is a more reproducible example. 我使用了
iris
数据,因为它是一个更可重现的示例。 The idea is to gather
the data. 这个想法是
gather
数据。 Then do the grouping and calculations. 然后进行分组和计算。 Afterwards you can
spread
the data back again. 之后,您可以再次
spread
数据。 To get the skewness per row you can use: 要获取每行的偏斜度,可以使用:
iris %>%
gather(key, value, -Species) %>%
group_by(Species) %>%
summarise(skew2=moments::skewness(value))
# A tibble: 3 x 2
Species skew2
<fct> <dbl>
1 setosa 0.146
2 versicolor 0.157
3 virginica 0.128
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.