我有4栏:车辆ID,车辆类别,车辆长度和车辆宽度。每辆车都有一个独特的车辆ID (例如2,4,5,.)每0.1秒收集一次数据,这意味着在车辆ID列中重复车辆ID的次数。在车辆类列中有三个车辆类,即1=motorcycles、2=cars、3=trucks,每一个车辆ID的长度和宽度都在它们各自的列中,我想根据车辆类对数据进行子集,然后在每个类别中找到每个车型的比例(唯一的长度和宽度)。例如,对于车辆类别=2,即汽车,我想找出不同型号的汽车(独特的长度和宽度)及其在汽车总数中所占的比例。以下是我迄今所做的工作:
按车辆类别分类数据
cars <- subset(b, b$'Vehicle class'==2)
trucks <- subset(b, b$'Vehicle class'==3)
motorcycles <- subset(b, b$'Vehicle class'==1)找出汽车的数量
numofcars <- length(unique(cars$'Vehicle ID')) # 2830
numoftrucks <- length(unique(trucks$'Vehicle ID')) # 137
numofmotorcycles <- length(unique(motorcycles$'Vehicle ID'))# 45上面的代码起作用了,但是我无法通过使用下面的代码找到比例:
by (cars, INDICES=cars$'Vehicle Length', FUN=table(cars$'Vehicle width'))R给出了一个错误,说明它找不到‘乐趣’。请帮我找出每种车型在各类车辆中所占的比例。
编辑(样本输入)
Vehicle ID Vehicle Class Vehicle Length Vehicle Width
2 2 13.5 4.5
2 2 13.5 4.5
2 2 13.5 4.5
2 2 13.5 4.5
3 2 13.5 4.0
3 2 13.5 4.0
3 2 13.5 4.0
3 2 13.5 4.0
4 2 10.0 4.5
4 2 10.0 4.5
4 2 10.0 4.5
4 2 10.0 4.5
5 3 23.0 4.5
5 3 23.0 4.5
5 3 23.0 4.5
5 3 23.0 4.5
6 3 76.5 4.5
6 3 76.5 4.5
6 3 76.5 4.5
6 3 76.5 4.5
6 3 76.5 4.5
7 1 10.0 3.0
7 1 10.0 3.0
7 1 10.0 3.0
7 1 10.0 3.0
8 2 13.5 5.5
8 2 13.5 5.5
8 2 13.5 5.5
8 2 13.5 5.5注意,在这个输入中: cars=4、trucks=2、motorcycles=1的总数
样本输出
Group: cars
VehicleLength VehicleWidth Proportion
13.5 4.5 0.25
13.5 4.0 0.25
13.5 5.5 0.25
23.0 4.5 0.25
Group:trucks
VehicleLength VehicleWidth Proportion
23.0 4.5 0.5
76.0 4.5 0.5
Group: motorcycles
VehicleLength VehicleWidth Proportion
10.0 3.0 1.0发布于 2013-12-01 05:16:58
你应该给出样本输出和样本输入,这样就更容易回答了。据我所知,你想要这样的东西-
library(data.table)
dt <- data.table(df)
dt2 <- dt[,
list(ClassLengthWidthFreq = .N),
by = c('VehicleClass','VehicleLength','VehicleWidth')
]
dt2[,
ClassLengthWidthFreqProportion := ClassLengthWidthFreq / sum(ClassLengthWidthFreq),
by = 'VehicleClass'
]产出-
> dt2
VehicleClass VehicleLength VehicleWidth ClassLengthWidthFreq ClassLengthWidthFreqProportion
1: 2 13.5 4.5 4 0.2500000
2: 2 13.5 4.0 4 0.2500000
3: 2 10.0 4.5 4 0.2500000
4: 3 23.0 4.5 4 0.4444444
5: 3 76.5 4.5 5 0.5555556
6: 1 10.0 3.0 4 1.0000000
7: 2 13.5 5.5 4 0.2500000如果没有,请添加示例输出和示例输入。
https://stackoverflow.com/questions/20308630
复制相似问题