为什么子集在 R 中不起作用? [英] Why subset won't work in R?
问题描述
我有一个数据框vissim",其结构如下:
I have a data frame 'vissim' which has following structure:
> str(vissim)
'data.frame': 480 obs. of 12 variables:
$ Measur. : int 1 2 3 4 5 6 7 8 9 10 ...
$ from : int 100 100 100 100 100 100 100 100 100 100 ...
$ to : int 130 130 130 130 130 130 130 130 130 130 ...
$ Occup..Rate.Trucks : num 2.9 NA NA NA NA 1 NA NA NA NA ...
$ Speed.Mean.Trucks : num 51.4 NA NA NA NA 50.7 NA NA NA NA ...
$ Number.Veh.Trucks : int 2 NA NA NA NA 1 NA NA NA NA ...
$ Occup..Rate.Motorcycles: num NA 0.7 NA NA NA NA NA 0.4 NA NA ...
$ Speed.Mean.Motorcycles : num NA 57.7 NA NA NA NA NA 64.2 NA NA ...
$ Number.Veh.Motorcycles : int NA 2 NA NA NA NA NA 1 NA NA ...
$ Occup..Rate.Car : num 6.8 3.3 NA NA NA 5.6 2.3 1.2 0.4 0.6 ...
$ Speed.Mean.Car : num 58.2 57.5 NA NA NA 60 63.5 64.5 60 55.6 ...
$ Number.Veh.Car : int 12 6 NA NA NA 11 5 2 1 1 ...
当我使用Measur"对其进行子集化时.变量以及以下标准,它工作正常:
When I subset it using 'Measur.' variable along with following criteria, it works fine:
> updatav <- subset(vissim, Measur.==c(1,2,3,4,5))
但是当我再次使用完全相同的变量但不同的标准进行子集化时,会出现以下错误:
But when I subset again using the very same variable but different criteria, it gives following error:
> bwdatav <- subset(vissim, Measur.==c(6,7,8,9,10,11))
> head(bwdatav)
[1] Measur. from to Occup..Rate.Trucks
[5] Speed.Mean.Trucks Number.Veh.Trucks Occup..Rate.Motorcycles Speed.Mean.Motorcycles
[9] Number.Veh.Motorcycles Occup..Rate.Car Speed.Mean.Car Number.Veh.Car
<0 rows> (or 0-length row.names)
不知道为什么当数据框vissim"明确包含Measur"的行时,它会说0行".6 到 11.有什么想法吗?
No idea why it is saying '0 rows' when the data frame 'vissim' clearly has rows for 'Measur.' 6 to 11. Any idea?
推荐答案
当您应该使用 %in%
时,您却使用了 ==
:
You are using ==
when you should be using %in%
:
updatav <- subset(vissim, Measur. %in% c(1,2,3,4,5))
(与子集真的无关.)
==
意味着相等,度量"中没有任何内容.列将等于该顺序中的所有值.
==
implies equality and nothing in the 'Measure.' column will equal all of those values in that order.
这篇关于为什么子集在 R 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!