簡體   English   中英

使用一定范圍的列(R)將函數應用於每一行

[英]Applying function to every row using a range of columns (R)

我的數據包含連續的列V1-V1998,而在這些列的任一側都有其他列。 我想計算此1998列范圍內的行的偏度。

這是我嘗試的代碼:

ND2a <- NoDup2 %>%
  rowwise() %>%
  mutate(skew2 = skewness(V1:V1998))

這將創建一個名為skew2的新列,但是不會計算偏斜度,而是用“ NaN”填充該列。 有誰知道為什么會這樣嗎?

我正在使用“時刻”包中的偏度。

我的數據看起來像這樣

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

你可以做:

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)

這樣,我們就可以計算給定數據集中所有列的每一行的偏斜度。

我會嘗試,但要取決於您以后要做什么。

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

我使用了iris數據,因為它是一個更可重現的示例。 這個想法是gather數據。 然后進行分組和計算。 之后,您可以再次spread數據。 要獲取每行的偏斜度,可以使用:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM