[R] How to get the proportions of data with respect to two variables in R?
Bert Gunter
gunter.berton at gene.com
Sun Dec 1 08:16:15 CET 2013
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
More information about the R-help
mailing list