R語言基礎入門(5) rowwise按行進行數(shù)據處理

本節(jié)來介紹tdplyr中的一個重要函數(shù)rowwise,可以通過它對數(shù)據按行進行處理
https://dplyr.tidyverse.org/articles/rowwise.html

依然還是使用我們熟悉的iris數(shù)據集

library(tidyverse)
iris %>% as_tibble()
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 

可以看到有5列其中一列為因子其余四列為數(shù)值,進行計算時只需要數(shù)值列

按行計算均值

方法1

iris %>%  as_tibble() %>% select(-Species) %>%
  mutate(.,mean=rowMeans(.))
   Sepal.Length Sepal.Width Petal.Length Petal.Width  mean
          <dbl>       <dbl>        <dbl>       <dbl> <dbl>
 1          5.1         3.5          1.4         0.2  2.55
 2          4.9         3            1.4         0.2  2.38
 3          4.7         3.2          1.3         0.2  2.35

方法2

根據邏輯判斷只選擇了數(shù)值列

iris %>% as_tibble() %>% select_if(is.numeric) %>%
  mutate(.,mean=rowMeans(.))
   Sepal.Length Sepal.Width Petal.Length Petal.Width  mean
          <dbl>       <dbl>        <dbl>       <dbl> <dbl>
 1          5.1         3.5          1.4         0.2  2.55
 2          4.9         3            1.4         0.2  2.38
 3          4.7         3.2          1.3         0.2  2.35

方法3

iris %>% as_tibble() %>% select_if(is.numeric) %>% 
  rowwise() %>% 
  mutate(mean = mean(c_across(Sepal.Length:Petal.Width)))

通過rowwise說明對數(shù)據按行進行處理,c_across選擇多列

方法4

iris %>% as_tibble() %>% 
  rowwise() %>%
  mutate(mean = rowMeans(across(where(is.numeric))))

通過across函數(shù)只選擇了數(shù)值列,此函數(shù)異常強大,靈活使用能使代碼簡潔無比

按行統(tǒng)計最小值

iris %>% as_tibble() %>% 
  rowwise() %>%
  mutate(min = min(across(where(is.numeric))))
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   min
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
 1          5.1         3.5          1.4         0.2 setosa    0.2
 2          4.9         3            1.4         0.2 setosa    0.2
 3          4.7         3.2          1.3         0.2 setosa    0.2
 4          4.6         3.1          1.5         0.2 setosa    0.2

按行統(tǒng)計最大值

iris %>% as_tibble() %>%
  rowwise() %>%
  mutate(max = max(across(where(is.numeric))))
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   max
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
 1          5.1         3.5          1.4         0.2 setosa    5.1
 2          4.9         3            1.4         0.2 setosa    4.9
 3          4.7         3.2          1.3         0.2 setosa    4.7
 4          4.6         3.1          1.5         0.2 setosa    4.6

按行求和

iris %>% as_tibble() %>%
  rowwise() %>%
  mutate(sum = sum(across(where(is.numeric))))
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   sum
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
 1          5.1         3.5          1.4         0.2 setosa   10.2
 2          4.9         3            1.4         0.2 setosa    9.5
 3          4.7         3.2          1.3         0.2 setosa    9.4
 4          4.6         3.1          1.5         0.2 setosa    9.4

按行計算標準差

iris %>% as_tibble() %>%
  rowwise() %>%
  mutate(sd = sd(across(where(is.numeric))))
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species    sd
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
 1          5.1         3.5          1.4         0.2 setosa   2.18
 2          4.9         3            1.4         0.2 setosa   2.04
 3          4.7         3.2          1.3         0.2 setosa   2.00
 4          4.6         3.1          1.5         0.2 setosa   1.91

統(tǒng)計每行中某值出現(xiàn)的次數(shù)

iris %>%  as_tibble() %>% select(-Species) %>%
  mutate(.,n=rowSums(. > 3))
   Sepal.Length Sepal.Width Petal.Length Petal.Width     n
          <dbl>       <dbl>        <dbl>       <dbl> <dbl>
 1          5.1         3.5          1.4         0.2     2
 2          4.9         3            1.4         0.2     1
 3          4.7         3.2          1.3         0.2     2
 4          4.6         3.1          1.5         0.2     2
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容