Hi, I hope this isn't a really simple question, I've been struggling with it for a while. I'm looking for a way to get a function to go through a data frame line by line, compare fields, and produce a result, kind of a transform and an if statement combined (I tried to put them together and it didn't work). So, consider data Sales: House number Inspected Sold 1 9/2/2011 10/10/2011 2 9/4/2011 10/20/2011 3 10/31/2011 8/28/2011 4 8/3/2011 11/1/2011 I want to find all the records which were inspected after they were sold. Ideally, this code would create a fourth field that would be a logical. I tried Sales<-transform(Sales, Checked=if(Sales$Inspected <= Sales$Sold) "OK") But that busted. I've bent over backward to make these kinds of comparisons work, but there has to be a better way. Any help would be most welcome. Thanks, Eric [[alternative HTML version deleted]]
Hi Eric,
Try
# data
x <- structure(list(House_number = 1:4, Inspected = structure(c(3L,
4L, 1L, 2L), .Label = c("10/31/2011", "8/3/2011",
"9/2/2011",
"9/4/2011"), class = "factor"), Sold = structure(c(1L, 2L,
4L,
3L), .Label = c("10/10/2011", "10/20/2011",
"11/1/2011", "8/28/2011"
), class = "factor")), .Names = c("House_number",
"Inspected",
"Sold"), class = "data.frame", row.names = c(NA, -4L))
> with(x, as.Date(Inspected, "%m/%d/%y") - as.Date(Sold,
"%m/%d/%y") > 0)
[1] FALSE FALSE TRUE FALSE
> x[with(x, as.Date(Inspected, "%m/%d/%y") - as.Date(Sold,
"%m/%d/%y") >
0), ]
House_number Inspected Sold
3 3 10/31/2011 8/28/2011
See ?as.Date for more information.
HTH,
Jorge.-
On Mon, Dec 26, 2011 at 7:52 PM, Eric Wolff <> wrote:
> Hi,
>
> I hope this isn't a really simple question, I've been struggling
with it
> for a while.
>
> I'm looking for a way to get a function to go through a data frame line
by
> line, compare fields, and produce a result, kind of a transform and an if
> statement combined (I tried to put them together and it didn't work).
>
> So, consider data Sales:
>
> House number Inspected Sold
> 1 9/2/2011 10/10/2011
> 2 9/4/2011 10/20/2011
> 3 10/31/2011 8/28/2011
> 4 8/3/2011 11/1/2011
>
> I want to find all the records which were inspected after they were sold.
> Ideally, this code would create a fourth field that would be a logical.
>
> I tried
>
> Sales<-transform(Sales, Checked=if(Sales$Inspected <= Sales$Sold)
"OK")
>
> But that busted. I've bent over backward to make these kinds of
comparisons
> work, but there has to be a better way. Any help would be most welcome.
>
> Thanks,
>
> Eric
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
On Dec 26, 2011, at 7:52 PM, Eric Wolff wrote:> Hi, > > I hope this isn't a really simple question, I've been struggling > with it > for a while. > > I'm looking for a way to get a function to go through a data frame > line by > line, compare fields, and produce a result, kind of a transform and > an if > statement combined (I tried to put them together and it didn't work). > > So, consider data Sales: > > House number Inspected Sold > 1 9/2/2011 10/10/2011 > 2 9/4/2011 10/20/2011 > 3 10/31/2011 8/28/2011 > 4 8/3/2011 11/1/2011 > > I want to find all the records which were inspected after they were > sold. > Ideally, this code would create a fourth field that would be a > logical. > > I tried > > Sales<-transform(Sales, Checked=if(Sales$Inspected <= Sales$Sold) > "OK")dat <- within(x, Checked <- as.Date(Inspected, format="%m/%d/%Y") < as.Date(Sold, format="%m/%d/%Y")) Note: it might have been easier if you had converted those string or factor variables into R Date variables. -- David Winsemius, MD West Hartford, CT
Possibly Parallel Threads
- Better way to count Active Record Data?
- Asterisk success stories in small-mediumoffice environments?
- FW: Register Today for Fall 2005 VON: "The Destination for IP Communications"
- One Cooperation invited from China
- grep or other complex string matching approach to capture necessary information...