简体   繁体   中英

summing up the first 5 elements of a list

I have a data frame that contains a column with varying numbers of integer values. I need to take the first five of these values and sum them up. I found a way to do it for one, but can't seem to generalize it to loop through all:

数据框摘录

Here is the code for the first element:

results$occupied[1] %>%
  strsplit(",") %>%
  as.list() %>%
  unlist() %>%
  head(5) %>%
  as.numeric() %>%
  sum()

And what does not work for all elements:

results %>%
  rowwise() %>%
  select(occupied) %>%
  as.character() %>%
  strsplit(",") %>%
  as.list() %>%
  unlist() %>%
  head(5) %>%
  as.numeric() %>%
  sum()

In base R, you can do:

sapply(strsplit(results$occupied, ","), function(x) sum(as.numeric(head(x, 5))))

Or using dplyr and purrr

library(dplyr)
library(purrr)

results %>%
    mutate(total_sum = map_dbl(strsplit(occupied, ","), 
                        ~sum(as.numeric(head(.x, 5)))))

Similarly, using rowwise:

results %>%
   rowwise() %>%
   mutate(total_sum = sum(as.numeric(head(strsplit(occupied, ",")[[1]], 5))))

We can use separate_rows to split the 'occupied' column and expand the rows, then do a group by row number and get the sum of the first five elements

library(dplyr)
library(tidyr)
results %>%
   mutate(rn = row_number()) %>%
   separate_rows(occupied, convert = TRUE) %>%
   group_by(rn) %>%
   slice(seq_len(5)) %>%
   summmarise(total_sum = sum(occupied)) %>%
   select(-rn) %>%
   bind_cols(results, .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM