0
votes

I am using the inbuilt airquality dataset in R Below is my code

f <- function(x){
             list(sd(x),median(x))
           }

 sapply(airquality,f)   

#output
  Ozone    Solar.R Wind     Temp     Month    Day     
 33.27597 91.1523 3.557713 9.529969 1.473434 8.707194
 31       207     9.7      79       7        16      

I want the same output but using purrr::map. I tried -

map_dbl(airquality,.f)

But this does not work. How do I fix this code?

4
There are multiple ways to do this. Can you post an example how you want the result to look like. So just type in manually how you want the result to be printed by R - akraf
Have edited the question illustrating the desired result - Manasi

4 Answers

6
votes

You can apply multiple functions using map_df/map_dfc based on how you want the output.

purrr::map_df(airquality, ~list(med = median(.x, na.rm = TRUE), sd = sd(.x, na.rm = TRUE)))

To get expected output exactly as shown we can use

purrr::map(airquality, f) %>%
    purrr::transpose() %>%
    dplyr::bind_rows()

# A tibble: 2 x 6
#  Ozone Solar.R  Wind  Temp Month   Day
#  <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl>
#1  33.3    91.2  3.56  9.53  1.47  8.71
#2  31     207    9.7  79     7    16   
5
votes

Base R:

sapply(airquality, function(x){

    list(std_dev = sd(x, na.rm = TRUE), med = median(x, na.rm = TRUE))

  }
)   
3
votes

You can do:

airquality %>% summarise_all(.funs = list(m = median, s = sd))

You get a data set with new column names that look like VAR_X, where VAR one of the old column names, and X is m or s, depending on whether that column is a median or an SD.

  Ozone_m Solar.R_m Wind_m Temp_m Month_m Day_m  Ozone_s Solar.R_s   Wind_s
1      31       207    9.7     79       7    16 33.27597   91.1523 3.557713
    Temp_s  Month_s    Day_s
1 9.529969 1.473434 8.707194
2
votes

We can uselapply in base R

do.call(cbind, lapply(airquality, function(x) c(std_dev = sd(x, na.rm = TRUE),
             med = median(x, na.rm = TRUE))))
#       Ozone   Solar.R     Wind     Temp    Month      Day
#std_dev 32.98788  90.05842 3.523001  9.46527 1.416522  8.86452
#med     31.50000 205.00000 9.700000 79.00000 7.000000 16.00000

Or using data.table

library(data.table)
as.data.table(airquality)[, lapply(.SD, function(x)
   c(std_dev = sd(x, na.rm = TRUE), med = median(x,  na.rm = TRUE)))]