0

I have a dataset with a column of lists of numbers. I want to mutate a column that has the sum of elements of each row's list. I tried this:

data %>% mutate(duration_sum = sum(unlist(Durations)))

But the duration_sum column values are all NA. Why is that and how can I fix this?

Mina
  • 327
  • 1
  • 6
  • 1
    You need to post a reproducible example of your data. But in the absence of that, did you try `sum(unlist(Durations)), na.rm = TRUE)`? – SamR Mar 15 '22 at 16:17
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Are there NA values in your list? Maybe use `na.rm=TRUE` in your call to `sum()` – MrFlick Mar 15 '22 at 16:17
  • 1
    If it is each row, `unlist/sum` returns a single value. You may need `data %>% mutate(duration_sum = purrr::map_dbl(Durations, sum, na.rm = TRUE))` – akrun Mar 15 '22 at 16:19
  • @SamR Thanks I tried it and it fixed my code. But it didn't sum each row. It summed all the values in the column! Why is that? – Mina Mar 15 '22 at 18:27
  • It would be a lot easier to help if you'd post some sample data. At a minimum, show us the output from `dput(head(data))`. – rdelrossi Mar 15 '22 at 21:25

0 Answers0