How can I use `mutate_at` for list-columns in a tibble?

lethalSinger

I have a tibble structured like this:

df <- 
  tibble(
    x = 1:3, 
    light_93 = list(1:3, 5:7, 18:20),
    light_94 = list(3:5, 9:11, 18:20),
    light_95 = list(5:7, 44:46, 30:32))

I want to make multiple new columns, giving the mean of each light_ column-list. So I want this result:

out <- 
  df %>% 
  mutate(light_93_mean = map_dbl(light_93, mean),
         light_94_mean = map_dbl(light_94, mean),
         light_95_mean = map_dbl(light_95, mean))

Can I use mutate_at to automate this? (I have hundreds of list-columns.) I can't figure out how to make it work in a tibble.

akrun

Specify the columns to apply in vars argument in mutate_at, then in each column loop over the list with map and get the mean

library(dplyr)
library(purrr)
df %>%
     mutate_at(vars(starts_with('light')), 
        list(mean = ~ map_dbl(., mean)))
# A tibble: 3 x 7
#      x light_93  light_94  light_95  light_93_mean light_94_mean light_95_mean
#  <int> <list>    <list>    <list>            <dbl>         <dbl>         <dbl>
#1     1 <int [3]> <int [3]> <int [3]>             2             4             6
#2     2 <int [3]> <int [3]> <int [3]>             6            10            45
#3     3 <int [3]> <int [3]> <int [3]>            19            19            31

Or using the devel version with across and mutate

df %>% 
     mutate(across(starts_with('light'), ~ map_dbl(., mean), names = "{col}_mean"))
# A tibble: 3 x 7
#      x light_93  light_94  light_95  light_93_mean light_94_mean light_95_mean
#  <int> <list>    <list>    <list>            <dbl>         <dbl>         <dbl>
#1     1 <int [3]> <int [3]> <int [3]>             2             4             6
#2     2 <int [3]> <int [3]> <int [3]>             6            10            45
#3     3 <int [3]> <int [3]> <int [3]>            19            19            31

It is also possible to have different sets of columns with different functions applied

df %>% 
    mutate(across(starts_with('light'), ~ map_dbl(., mean), names = "{col}_mean"),
           across(matches('(94|95)$'), ~ map_dbl(., sum), names = "{col}_sum"))
# A tibble: 3 x 9
#      x light_93  light_94  light_95  light_93_mean light_94_mean light_95_mean light_94_sum light_95_sum
#  <int> <list>    <list>    <list>            <dbl>         <dbl>         <dbl>        <dbl>        <dbl>
#1     1 <int [3]> <int [3]> <int [3]>             2             4             6           12           18
#2     2 <int [3]> <int [3]> <int [3]>             6            10            45           30          135
#3     3 <int [3]> <int [3]> <int [3]>            19            19            31           57           93

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I use CSS dot leaders for a "three column" list?

분류에서Dev

pasting variables into a mutate_at function to mutate specific columns if they exist

분류에서Dev

Use Parse.com and Android, how can I get a list of Games that a pair of Players share?

분류에서Dev

How can I get a complete list of non-standard repositories in use?

분류에서Dev

How i can use a criteria in cicle for i

분류에서Dev

How can I create List of rectangles in TypeScript?

분류에서Dev

How can I centralize a list with css?

분류에서Dev

How can I create a resource list on Apigility?

분류에서Dev

How can I return a list of summary statistics?

분류에서Dev

How do i refer to rows/ columns in data frames that are within a list?

분류에서Dev

tibble의 목록 열에`mutate_at`를 어떻게 사용할 수 있습니까?

분류에서Dev

How can I use the selected category in a databaseQueryProcessor?

분류에서Dev

How can I use onItemSelected in Android?

분류에서Dev

How can I use relative path in CodeIgniter?

분류에서Dev

How can I use CUDA without the monitor?

분류에서Dev

How can I use sharedPreferences in baseAdapter?

분류에서Dev

How can I use typescript in Aptana Studio?

분류에서Dev

How can I use progressbar in Android

분류에서Dev

How long can i use WSAPI 1.42

분류에서Dev

How can i use data in an array in a query

분류에서Dev

How can i use $_POST if the name is a variable?

분류에서Dev

How can I use another drive with Owncloud?

분류에서Dev

How can I use LVM snapshots in Ubuntu?

분류에서Dev

How can I use my backup of Thunderbird

분류에서Dev

How can I use vector for entering numbers?

분류에서Dev

Unnest multiple columns-list from tibble with tidyr unnes_wider

분류에서Dev

Where can I get a list of SCHEMA / PATH / KEY to use with gsettings?

분류에서Dev

How can I set the number of columns in a listview dynamically?

분류에서Dev

How can I create 3 columns using UICollectionView

Related 관련 기사

  1. 1

    How can I use CSS dot leaders for a "three column" list?

  2. 2

    pasting variables into a mutate_at function to mutate specific columns if they exist

  3. 3

    Use Parse.com and Android, how can I get a list of Games that a pair of Players share?

  4. 4

    How can I get a complete list of non-standard repositories in use?

  5. 5

    How i can use a criteria in cicle for i

  6. 6

    How can I create List of rectangles in TypeScript?

  7. 7

    How can I centralize a list with css?

  8. 8

    How can I create a resource list on Apigility?

  9. 9

    How can I return a list of summary statistics?

  10. 10

    How do i refer to rows/ columns in data frames that are within a list?

  11. 11

    tibble의 목록 열에`mutate_at`를 어떻게 사용할 수 있습니까?

  12. 12

    How can I use the selected category in a databaseQueryProcessor?

  13. 13

    How can I use onItemSelected in Android?

  14. 14

    How can I use relative path in CodeIgniter?

  15. 15

    How can I use CUDA without the monitor?

  16. 16

    How can I use sharedPreferences in baseAdapter?

  17. 17

    How can I use typescript in Aptana Studio?

  18. 18

    How can I use progressbar in Android

  19. 19

    How long can i use WSAPI 1.42

  20. 20

    How can i use data in an array in a query

  21. 21

    How can i use $_POST if the name is a variable?

  22. 22

    How can I use another drive with Owncloud?

  23. 23

    How can I use LVM snapshots in Ubuntu?

  24. 24

    How can I use my backup of Thunderbird

  25. 25

    How can I use vector for entering numbers?

  26. 26

    Unnest multiple columns-list from tibble with tidyr unnes_wider

  27. 27

    Where can I get a list of SCHEMA / PATH / KEY to use with gsettings?

  28. 28

    How can I set the number of columns in a listview dynamically?

  29. 29

    How can I create 3 columns using UICollectionView

뜨겁다태그

보관