使用一系列列(R)将函数应用于每一行

时间:2021-02-26 13:21:05

My data contains consecutive columns V1-V1998 with other columns at either side of these. I want to calculate the skewness of the rows within this range of 1998 columns.

我的数据包含连续的列V1-V1998,其中包含其他列。我想计算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". Does anyone know why this might be?

这将创建一个名为skew2的新列,但不会计算偏度,而是使用“NaN”填充该列。有谁知道为什么会这样?

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

2 个解决方案

#1


1  

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.

有了这个,我们计算给定数据集中所有列的每一行的偏度。

#2


1  

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. The idea is to gather the data. Then do the grouping and calculations. Afterwards you can spread the data back again. 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

#1


1  

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.

有了这个,我们计算给定数据集中所有列的每一行的偏度。

#2


1  

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. The idea is to gather the data. Then do the grouping and calculations. Afterwards you can spread the data back again. 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