Ganjam Sreelasya
Ganjam Sreelasya

Reputation: 11

Data handling in R; Data tables

Is there any alternative solution for the question?

  1. Create a Data Table using the R built-in dataset mtcars

  2. Find the average-weight(wt) & average horse-power(hp) of all cars with a mileage above 15 mpg, grouped by number of cylinders(cyl) and carburettors(carb).

    s<-data.table(mtcars)
    s[mpg>15,.(mean(wt),mean(hp)),by=.(cyl,carb)]

Upvotes: 0

Views: 1231

Answers (2)

user14731228
user14731228

Reputation: 11

#If is for Fresco you need to add the library

library(data.table)
s<-data.table(mtcars)
  s[mpg>15,.(mean(wt),mean(hp)),by=.(cyl,carb)]

Upvotes: 1

holzben
holzben

Reputation: 1471

A dpylr solution would be:

require(dplyr)
as_data_frame(mtcars) %>% 
  filter(mpg > 15) %>% 
  group_by(cyl, carb) %>%
  summarise(mean_wt = mean(wt),
            mean_hb = mean(hp))

Upvotes: 0

Related Questions