can i calculate a variance specifically by a column and rows

时间:2019-04-17 01:34:28

标签: r

What is the variance for the Driver_Height variable in the UPS group?

head(Delivery_data)

  Company Box_Weight Driver_Height Driver_Salary
1     UPS       14.3            63         45144
2     UPS       16.8            65         44986
3     UPS       14.0            63         45798
4     UPS       17.8            59         44581
5     UPS       13.5            66         44912
6     UPS       13.3            64         44809

Im very new to R, Just need to sort UPS and Ht data to calc VAR.

There are 3 treatments otherwise I could create an object for HT and do the calculation. Cheers!!

1 个答案:

答案 0 :(得分:0)

I think you are trying to ask if you can obtain variance of different columns, for a subset of rows at a time?

#library(tidyverse)
Delivery_data %>% group_by(Company) %>% 
summarize("Height.var" = var(Driver_Height), "Salary.var" = var(Driver_Salary))

You can add or remove variables for which you do or don't want to compute a variance as above. A shortcut if you quickly want the variance of one column (and one subset of rows) is to use the [] brackets:

var(Delivery_data[Delivery_data$Company == "UPS", "Driver_Height"])