[R] Remove Rows Based on Factor

arun smartpink111 at yahoo.com
Mon Apr 15 19:59:36 CEST 2013


Hi,
In addition:
You can subset based on time or date:
ZZ1['2006']
#           Open High  Low Close   Volume Adjusted
#2006-04-07 17.5 18.2 17.3  17.5 23834500     16.8
#2006-04-08 17.6 17.6 16.8  16.8  2916000     16.2


ZZ1['2007-01/2008-01']
#           Open High  Low Close  Volume Adjusted
#2007-01-01 16.8 17.2 16.8  17.1 1453700     16.4
#2007-01-01 17.2 17.2 16.8  16.8  991400     16.2
#2008-01-04 17.3 17.5 16.2  15.9  443522     16.5
#2008-01-04 17.5 17.6 15.8  16.2   38990     17.6
 ZZ1['2007/2008']
#           Open High  Low Close  Volume Adjusted
#2007-01-01 16.8 17.2 16.8  17.1 1453700     16.4
#2007-01-01 17.2 17.2 16.8  16.8  991400     16.2
#2008-01-04 17.3 17.5 16.2  15.9  443522     16.5
#2008-01-04 17.5 17.6 15.8  16.2   38990     17.6



ZZ2<- data.frame(Index=c("2006-04-07 02:30:00","2006-04-08 02:00:00","2007-01-01 01:30:00","2007-01-01 02:30:00","2008-01-04 03:00:00","2008-01-04 03:30:00"),Open=c(17.5,17.6,16.8,17.2,17.3,17.5),High=c(18.2,17.6,17.2,17.2,17.5,17.6),Low=c(17.3,16.8,16.8,16.8,16.2,15.8),Close=c(17.5,16.8,17.1,16.8,15.9,16.2),Volume=c(23834500,2916000,1453700,991400,443522,38990),Adjusted=c(16.8,16.2,16.4,16.2,16.5,17.6),stringsAsFactors=FALSE)
ZZ2$Index<- as.POSIXct(ZZ2$Index,format="%Y-%m-%d %H:%M:%S")
library(xts)
 ZZ3<-xts(ZZ2[,-1],ZZ2[,1])
 ZZ3["T02:30/T03:00"]
#                    Open High  Low Close   Volume Adjusted
#2006-04-07 02:30:00 17.5 18.2 17.3  17.5 23834500     16.8
#2007-01-01 02:30:00 17.2 17.2 16.8  16.8   991400     16.2
#2008-01-04 03:00:00 17.3 17.5 16.2  15.9   443522     16.5
A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: "Sparks, John James" <jspark4 at uic.edu>
Cc: R help <r-help at r-project.org>
Sent: Monday, April 15, 2013 1:32 PM
Subject: Re: [R] Remove Rows Based on Factor

Hi,
May be this helps:
ZZ<- data.frame(Index=c("2006-04-07","2006-04-08","2007-01-01","2007-01-01","2008-01-04","2008-01-04"),Open=c(17.5,17.6,16.8,17.2,17.3,17.5),High=c(18.2,17.6,17.2,17.2,17.5,17.6),Low=c(17.3,16.8,16.8,16.8,16.2,15.8),Close=c(17.5,16.8,17.1,16.8,15.9,16.2),Volume=c(23834500,2916000,1453700,991400,443522,38990),Adjusted=c(16.8,16.2,16.4,16.2,16.5,17.6))
ZZ$Index<- as.Date(ZZ$Index,format="%Y-%m-%d")
library(xts)
 ZZ1<-xts(ZZ[,-1],ZZ[,1])
ZZ1
#           Open High  Low Close   Volume Adjusted
#2006-04-07 17.5 18.2 17.3  17.5 23834500     16.8
#2006-04-08 17.6 17.6 16.8  16.8  2916000     16.2
#2007-01-01 16.8 17.2 16.8  17.1  1453700     16.4
#2007-01-01 17.2 17.2 16.8  16.8   991400     16.2
#2008-01-04 17.3 17.5 16.2  15.9   443522     16.5
#2008-01-04 17.5 17.6 15.8  16.2    38990     17.6

Suppose, I wanted to get only the rows starting from "2006-04-08"
 ZZ1['2006-04-08/']
#           Open High  Low Close  Volume Adjusted
#2006-04-08 17.6 17.6 16.8  16.8 2916000     16.2
#2007-01-01 16.8 17.2 16.8  17.1 1453700     16.4
#2007-01-01 17.2 17.2 16.8  16.8  991400     16.2
#2008-01-04 17.3 17.5 16.2  15.9  443522     16.5
#2008-01-04 17.5 17.6 15.8  16.2   38990     17.6
A.K.




----- Original Message -----
From: "Sparks, John James" <jspark4 at uic.edu>
To: r-help at r-project.org
Cc: 
Sent: Monday, April 15, 2013 12:58 PM
Subject: [R] Remove Rows Based on Factor

Dear R Helpers,

I did a search for deleting rows based on conditions but wasn't able to
find an example that addressed the error that I am getting.  I am hoping
that this is a simple syntax phenomenon that somebody else knows off the
top of their head.  My apologies for not providing a reproducible example
but I think that the information given will allow someone to give me a
hint.

I want to delete the rows of the data frame ZZ where Index is earlier that
Jan 1 of 2007.  That Index column is a factor.  When I tired a couple of
different methods, I got the error shown below.  Can anybody tell me what
I am doing wrong?  I would really appreciate it.

--John Sparks

> str(ZZ)
'data.frame':   1584 obs. of  7 variables:
$ Index   : Factor w/ 1583 levels "2006-04-07","2006-04-10",..: 1 2 3 4 5
6 7 8 9 10 ...
$ Open    : num  17.5 17.6 16.8 17.2 17 ...
$ High    : num  18.2 17.6 17.2 17.2 17.1 ...
$ Low     : num  17.3 16.8 16.8 16.8 16.6 ...
$ Close   : num  17.5 16.8 17.1 16.8 16.7 ...
$ Volume  : num  23834500 2916000 1453700 991400 967400 ...
$ Adjusted: num  16.8 16.2 16.4 16.2 16 ...
> test<-ZZ[ZZ$Index>"2007-01-01",]
Warning message:
In Ops.factor(ZZ$Index, "2007-01-01") : > not meaningful for factors
>
> test<-subset(ZZ,Index>2007-01-01)
Warning message:
In Ops.factor(Index, 2007 - 1 - 1) : > not meaningful for factors

______________________________________________
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.



More information about the R-help mailing list