user123231322
user123231322

Reputation: 65

summing number of different columns

I want to return the list of data frames with the number of different columns apart from the name column, which I want to return the same. So for this example, it would return

list3$dfa

     names X  Y   Z
1    Ben   1  1   2

list3$dfb

   names  X  Y  Z
1  John   2  2  2

Kindly let me know how I would do this, please.

Upvotes: 0

Views: 31

Answers (2)

akrun
akrun

Reputation: 887501

We can use map2 from purrr

library(dplyr)
library(purrr)
map2(list1, list2, ~  
      tibble(Name = select(.x, names) %>% slice(1),
      map2_dfc(.x[-1], .y[-1],  ~ 100 *mean(.x != .y))))
#$dfa
# A tibble: 1 x 4
#  Name$names     X     Y     Z
#  <chr>      <dbl> <dbl> <dbl>
#1 Ben         33.3  33.3  66.7

#$dfb
# A tibble: 1 x 4
#  Name$names     X     Y     Z
#  <chr>      <dbl> <dbl> <dbl>
#1 John          50    50    75

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389135

You can use Map :

Map(function(x, y) data.frame(Name = x[[1]][1], 
                       t(colMeans(x[-1] != y[-1])) * 100), list1, list2)

#$dfa
#  Name    X    Y    Z
#1  Ben 33.3 33.3 66.7

#$dfb
#  Name  X  Y  Z
#1 John 50 50 75

Upvotes: 1

Related Questions