umair durrani
2013-Dec-01 04:11 UTC
[R] How to get the proportions of data with respect to two variables in R?
I have 4 columns: Vehicle ID, Vehicle Class, Vehicle Length and Vehicle Width. Every vehicle has a unique vehicle ID (e.g. 2, 4, 5,...) and the data was collected every 0.1 seconds which means that vehicle IDs are repeated in Vehicle ID column for the number of times they were observed. There are three vehicle classes i.e. 1=motorcycles, 2=cars, 3=trucks in the Vehicle Class column and the lengths and widths are in their respective columns against every vehicle ID. I want to subset the data by vehicle class and then find the proportions of each vehicle model (unique length and width) within every class. For example, for the Vehicle Class = 2 i.e. car, I want to find different models of cars (unique length and width) and their proportions with respect to total number of cars. Here is what I have done so far:To subset data by Vehicle Classcars <- subset(b, b$'Vehicle class'==2) trucks <- subset(b, b$'Vehicle class'==3) motorcycles <- subset(b, b$'Vehicle class'==1)To find the number of carsnumofcars <- length(unique(cars$'Vehicle ID')) # 2830 numoftrucks <- length(unique(trucks$'Vehicle ID')) # 137 numofmotorcycles <- length(unique(motorcycles$'Vehicle ID'))# 45The above code worked but I could not find the proportions by using the code below:by (cars, INDICES=cars$'Vehicle Length', FUN=table(class$'Vehicle width'))R gives an error stating that it could not find 'FUN'. Please help me in finding the proportions of each model within all classes of vehicles. Umair Durrani email: umairdurrani@outlook.com [[alternative HTML version deleted]]
Bert Gunter
2013-Dec-01 07:16 UTC
[R] How to get the proportions of data with respect to two variables in R?
1. A nice example of why posting a small reproducible example would help get you a useful reply. Your wordy description may not suffice. 2. You are requested not to post in HTML. This is a mailing list, not a web application. 3. table(class$'Vehicle width') is not a function; it is a table object returned by the table() function. You need to spend some additional effort learning R (lots of good tutorials out there and there's the Intro to R doc that shipes with R) so that you understand the difference. It clearly matters, no? 4. A stab at an answer (**untested in the absence of data**): Create a new variable, call it size, via: b <- within(b,size <- paste0(length, width) ) ## Use simpler names to avoid quotes ## Size is your unique model identifier -- maybe. Then with(b,table(ID,class,size)) ## should get you close, I think. Others may have better insight if this is not what you're after. Again, a small example would probably be helpful. Use data.frame() or dput() to include data in a post -- do NOT just type the data in. See the posting guide for details. Cheers, Bert On Sat, Nov 30, 2013 at 8:11 PM, umair durrani <umairdurrani at outlook.com> wrote:> I have 4 columns: Vehicle ID, Vehicle Class, Vehicle Length and Vehicle Width. Every vehicle has a unique vehicle ID (e.g. 2, 4, 5,...) and the data was collected every 0.1 seconds which means that vehicle IDs are repeated in Vehicle ID column for the number of times they were observed. There are three vehicle classes i.e. 1=motorcycles, 2=cars, 3=trucks in the Vehicle Class column and the lengths and widths are in their respective columns against every vehicle ID. I want to subset the data by vehicle class and then find the proportions of each vehicle model (unique length and width) within every class. For example, for the Vehicle Class = 2 i.e. car, I want to find different models of cars (unique length and width) and their proportions with respect to total number of cars. Here is what I have done so far:To subset data by Vehicle Classcars <- subset(b, b$'Vehicle class'==2) > trucks <- subset(b, b$'Vehicle class'==3) > motorcycles <- subset(b, b$'Vehicle class'==1)To find the number of carsnumofcars <- length(unique(cars$'Vehicle ID')) # 2830 > numoftrucks <- length(unique(trucks$'Vehicle ID')) # 137 > numofmotorcycles <- length(unique(motorcycles$'Vehicle ID'))# 45The above code worked but I could not find the proportions by using the code below:by (cars, INDICES=cars$'Vehicle Length', FUN=table(class$'Vehicle width'))R gives an error stating that it could not find 'FUN'. Please help me in finding the proportions of each model within all classes of vehicles. > > Umair Durrani > > email: umairdurrani at outlook.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374
arun
2013-Dec-01 07:41 UTC
[R] How to get the proportions of data with respect to two variables in R?
Hi, It is better to provide a reproducible example. May be this helps: set.seed(252) dat1 <- data.frame(`Vehicle ID`=sample(150,150,replace=FALSE),`Vehicle Class`=rep(1:4,c(20,40,30,60)), `Vehicle length`= sample(15:25,150,replace=TRUE), `Vehicle width`= sample(4:10,150,replace=TRUE),check.names=FALSE) cars <- subset(dat1,`Vehicle Class`==2) ?by(cars,INDICES=cars$`Vehicle length`,FUN=table(cars$`Vehicle width`))? #Error in FUN(X[[1L]], ...) : could not find function "FUN" by(cars$`Vehicle width`,INDICES=cars$`Vehicle length`, table) ?by(dat1$`Vehicle width`,list(dat1$`Vehicle Class`,dat1$`Vehicle length`), table) #Also, you may check ftable(dat1[2:4]) prop.table(ftable(dat1[2:4]),1) A.K. On Sunday, December 1, 2013 12:08 AM, umair durrani <umairdurrani at outlook.com> wrote: I have 4 columns: Vehicle ID, Vehicle Class, Vehicle Length and Vehicle Width. Every vehicle has a unique vehicle ID (e.g. 2, 4, 5,...) and the data was collected every 0.1 seconds which means that vehicle IDs are repeated in Vehicle ID column for the number of times they were observed. There are three vehicle classes i.e. 1=motorcycles, 2=cars, 3=trucks in the Vehicle Class column and the lengths and widths are in their respective columns against every vehicle ID. I want to subset the data by vehicle class and then find the proportions of each vehicle model (unique length and width) within every class. For example, for the Vehicle Class = 2 i.e. car, I want to find different models of cars (unique length and width) and their proportions with respect to total number of cars. Here is what I have done so far:To subset data by Vehicle Classcars <- subset(b, b$'Vehicle class'==2) trucks <- subset(b, b$'Vehicle class'==3) motorcycles <- subset(b, b$'Vehicle class'==1)To find the number of carsnumofcars <- length(unique(cars$'Vehicle ID')) # 2830 numoftrucks <- length(unique(trucks$'Vehicle ID')) # 137 numofmotorcycles <- length(unique(motorcycles$'Vehicle ID'))# 45The above code worked but I could not find the proportions by using the code below:by (cars, INDICES=cars$'Vehicle Length', FUN=table(class$'Vehicle width'))R gives an error stating that it could not find 'FUN'. Please help me in finding the proportions of each model within all classes of vehicles. Umair Durrani email: umairdurrani at outlook.com ??? ??? ??? ? ??? ??? ? ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.