根据R中2列中的值选择特定行

问题描述:

我有一个大型数据集的GPS衣领位置,每天有不同数量的位置。我想分开只收集一个位置的日子,并创建一个包含所有信息的新数据框。

I have a large data set of GPS collar locations that have a varying number of locations each day. I want to separate out only the days that have a single location collected and make a new data frame containing all their information.

month    day    easting    northing    time    ID
  6       1     #######    ########    0:00    ##
  6       2     #######    ########    6:00    ##
  6       2     #######    ########    0:00    ##
  6       3     #######    ########    18:00   ##
  6       3     #######    ########    12:00   ##
  6       4     #######    ########    0:00    ##
  6       5     #######    ########    6:00    ##

目前我已经集合了一些东西,但是不能进入下一步。

Currently I have hashed together something, but can't quite get to the next step.

library(plyr)
dog<-count(data1,vars=c("MONTH","day"))
datasub1<-subset(dog,freq==1)

这给我一个读数,看起来像

This gives me a readout that looks like

    MONTH day freq
1       6  29    1
7       7   5    1
8       7   6    1
10      7   8    1
12      7  10    1

我尝试使用月份和日期的值包含来自主数据集的行,以便我可以创建一个仅包含频率为1但包含所有关联数据的点的数据框。我已经到了这一步:

I am trying to use the values of the Month and day to pull out the rows that contain them from the main dataset so that I can make a data frame containing only the points with a frequency of 1 but that contains all the associated data. I've got to this point:

sis<-c(datasub1$MONTH)
bro<-c(datasub1$day)
datasub2<-subset(data1,MONTH==sis&day==bro)


$ b b

...但是这不会给我任何东西,个人而言,它使直觉有意义(R初学者),它应该包含两个值的兄弟和sis的行。

... but that doesn't give me anything, personally it makes intuitive sense (R beginner) that it should subset out the rows that contain both the values of bro and sis.

任何帮助将非常感激。

修订: / p>

Revised:

datasub2<-subset(data1, paste(month,day,sep=".") %in% paste(datasub1$MONTH, datasub1$day,sep=".") )

),任何特定的MONTH项目将完全等于该子集。您可能更感兴趣的是Month.Day的组合是否在datasub1中的Month.Day的组合集中。如果标题是您所说明的,那么您已经混合了从count()函数返回的大小写。

It's not very likely (and quite possibly impossible) that any particular MONTH item will exactly equal that subset. You are presumably more interested in whether a combo of "Month.Day" is in the combo sets of "Month.Day" in the datasub1. You have mixed up the capitalization that returns from the count() function if the headers were as you illustrated.

> dog
  month day freq
1     6   1    1
2     6   2    2
3     6   3    2
4     6   4    1
5     6   5    1
> datasub1
  month day freq
1     6   1    1
4     6   4    1
5     6   5    1
> datasub2
  month day easting northing time ID
1     6   1 ####### ######## 0:00 ##
6     6   4 ####### ######## 0:00 ##
7     6   5 ####### ######## 6:00 ##