簡體   English   中英

如何將transmute與grep function結合起來?

[英]How to combine transmute with grep function?

我正在嘗試使用現有 dataframe 中的rowSums() function 找到一種方法來創建帶有變量的新表。 例如,我現有的 dataframe 被稱為'asn' ,我想總結變量標題中包含“2011”的所有變量的每一行的值。 我想要一個只包含一個名為asn_y2011的列的新表,其中包含使用包含“2011”的變量的每一行的總和

數據

structure(list(row = 1:3, south_2010 = c(1L, 5L, 7L), south_2011 = c(4L, 
0L, 4L), south_2012 = c(5L, 8L, 6L), north_2010 = c(3L, 4L, 1L
), north_2011 = c(2L, 6L, 0L), north_2012 = c(1L, 1L, 2L)), class = "data.frame", row.names = c(NA, 
-3L))

現有'asn' dataframe 看起來像這樣

row south_2010 south_2011 south_2012 north_2010 north_2011 north_2012
  1      1           4         5          3          2          1
  2      5           0         8          4          6          1
  3      7           4         6          1          0          2

我正在嘗試使用以下 function:

asn %>%   
   transmute(asn_y2011 = rowSums(, grep("2011")))

得到這樣的東西

row    asn_y2011
 1         6
 2         6
 3         4

繼續您的代碼, grep()應該像這樣工作:

library(dplyr)

asn %>%
  transmute(row, asn_y2011 = rowSums(.[grep("2011", names(.))]))

#   row asn_y2011
# 1   1         6
# 2   2         6
# 3   3         4

或者您可以在c_across()中使用整潔的選擇

asn %>%
  rowwise() %>% 
  transmute(row, asn_y2011 = sum(c_across(contains("2011")))) %>%
  ungroup()

另一個使用rowSums基本 R 選項

cbind(asn[1],asn_y2011 = rowSums(asn[grep("2011",names(asn))]))

這使

  row asn_y2011
1   1         6
2   2         6
3   3         4

base R中的一個選項,帶有Reduce

cbind(df['row'], asn_y2011 = Reduce(`+`, df[endsWith(names(df), '2011')]))
#  row asn_y2011
#1   1         6
#2   2         6
#3   3         4

數據

df <- structure(list(row = 1:3, south_2010 = c(1L, 5L, 7L), south_2011 = c(4L, 
0L, 4L), south_2012 = c(5L, 8L, 6L), north_2010 = c(3L, 4L, 1L
), north_2011 = c(2L, 6L, 0L), north_2012 = c(1L, 1L, 2L)), 
class = "data.frame", row.names = c(NA, 
-3L))

我認為這段代碼會做你想做的事:

library(magrittr)
tibble::tibble(row = 1:3, south_2011 = c(4, 0, 4), north_2011 = c(2, 6, 0)) %>%
  tidyr::gather(- row, key = "key", value = "value") %>%
  dplyr::mutate(year = purrr::map_chr(.x = key, .f = function(x)stringr::str_split(x, pattern = "_")[[1]][2])) %>%
  dplyr::group_by(row, year) %>%
  dplyr::summarise(sum(value))

我首先加載 package magrittr ,以便我可以使用 pipe, %>% 我已經明確列出了從中導出函數的包,但是如果您願意,歡迎您使用library加載包。

然后,我創建一個 tibble 或數據框,就像您指定的那樣。

在創建新變量year之前,我使用gather重新組織數據框。 然后,我按rowyear的值匯總計數。

你可以試試這個方法

library(tidyverse)
df2 <- df %>% 
  select(grep("_2011|row", names(df), value = TRUE)) %>% 
  rowwise() %>% 
  mutate(asn_y2011 = sum(c_across(south_2011:north_2011))) %>% 
  select(row, asn_y2011)
  
#     row asn_y2011
#   <int>     <int>
# 1     1         6
# 2     2         6
# 3     3         4

數據

df <- structure(list(row = 1:3, south_2010 = c(1L, 5L, 7L), south_2011 = c(4L, 0L, 4L), south_2012 = c(5L, 8L, 6L), north_2010 = c(3L, 4L, 1L), north_2011 = c(2L, 6L, 0L), north_2012 = c(1L, 1L, 2L)), class = "data.frame", row.names = c(NA,-3L))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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