|
Hello,
I have to create a new column from the values of other columns of a data frame. The new column (y$n) is created imposing a condition (using a third variable y$h) that assigns the values of two time variables (y$b and y$timepos). Here's the piece of code to get there (using the attached files): xact <- read.table("act.lig", sep = ',', col.names=c("ok","time","secs","act")) xlig <- read.table("lig.txt", sep = ',', col.names=c("ok","time","secs","lig")) w<- merge(xact, xlig, by = c("time" ,"secs"), all = TRUE, sort=F) require(reshape) z <- cbind(w, colsplit(w$time, split=" ", names=c("date", "clock"))) zh<-cbind(z, colsplit(z$clock, split=":", names=c("h","m","s"))) zhd<- cbind(zh, colsplit(zh$date, split="/", names=c("d","mo","y"))) night <- subset(zhd, zh$lig<6 & zhd$h<9 | zh$lig<6 & zhd$h>21) night$timepos<-as.POSIXct(night$time, tz="GMT", format="%d/%m/%y %H:%M:%S") a=night$timepos - as.difftime( 1, units="days" ) nighta<-cbind(night,a) y<- cbind(nighta, b=as.character(a, tz= "GMT", format= "%Y-%m-%d")) y$n<-with(y, if (h>=0 & h<9) {b} else {timepos}) ## Missing warnings In if (h >= 0 & h < 9) { : condition has length > 1 and only the first element will be used How can I go around this problem and get the new column? Thank you, Santi ______________________________________________ [hidden email] 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. |
|
Bahhhh -- far too much work to recreate (and I don't think you sent us
the file "act.lig"): here's a much better route: Go to the step immediately before you're in trouble and use dput() on your data. R will print out a nice plaintext representation that we can copy and paste and reproduce *exactly* without having to do all that you show below. Incidentally, your warning message suggests you should be using ifelse() instead of if. To compare: x <- seq(-3, 3) abs.x.wrong <- if(x < 0) -x else x # Warning message gives some hint abs.x.right <- ifelse(x < 0, -x, x) Hope this helps, Michael On Sat, May 5, 2012 at 5:09 PM, Santiago Guallar <[hidden email]> wrote: > Hello, > > I have to create a new column from the values of other columns of a data frame. The new column (y$n) is created imposing a condition (using a third variable y$h) that assigns the values of two time variables (y$b and y$timepos). Here's the piece of code to get there (using the attached files): > > xact <- read.table("act.lig", sep = ',', col.names=c("ok","time","secs","act")) > xlig <- read.table("lig.txt", sep = ',', col.names=c("ok","time","secs","lig")) > w<- merge(xact, xlig, by = c("time" ,"secs"), all = TRUE, sort=F) > require(reshape) > z <- cbind(w, colsplit(w$time, split=" ", names=c("date", "clock"))) > zh<-cbind(z, colsplit(z$clock, split=":", names=c("h","m","s"))) > zhd<- cbind(zh, colsplit(zh$date, split="/", names=c("d","mo","y"))) > night <- subset(zhd, zh$lig<6 & zhd$h<9 | zh$lig<6 & zhd$h>21) > night$timepos<-as.POSIXct(night$time, tz="GMT", format="%d/%m/%y %H:%M:%S") > a=night$timepos - as.difftime( 1, units="days" ) > nighta<-cbind(night,a) > y<- cbind(nighta, b=as.character(a, tz= "GMT", format= "%Y-%m-%d")) > y$n<-with(y, if (h>=0 & h<9) {b} else {timepos}) ## Missing warnings > In if (h >= 0 & h < 9) { : > condition has length > 1 and only the first element will be used > > How can I go around this problem and get the new column? > > Thank you, > > Santi > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
|
Hi Michael,
Yes, I tried ifelse() before but this function returns a numeric value. When I try to convert it back to POSIXct I get a <NA>*. If I use if else I get a POSIXct output although it does not return a correct answer (it only returns y$timepos even when the condition "h<9" fails to be met). Attached the outputs you suggested (thanks, by the way, I didn't know dput()). Hope they go through this time. Thank you, Santi *niga$isnight<- as.POSIXct( niga$nit, tz="GMT", format="%Y-%m-%d %H:%M:%S", origin="2007-06-19" ) From: R. Michael Weylandt <[hidden email]> >To: Santiago Guallar <[hidden email]> >Cc: "[hidden email]" <[hidden email]> >Sent: Sunday, May 6, 2012 2:18 AM >Subject: Re: [R] creating a new column assigning values of other columns > >Bahhhh -- far too much work to recreate (and I don't think you sent us >the file "act.lig"): here's a much better route: > >Go to the step immediately before you're in trouble and use dput() on >your data. R will print out a nice plaintext representation that we >can copy and paste and reproduce *exactly* without having to do all >that you show below. > >Incidentally, your warning message suggests you should be using >ifelse() instead of if. > >To compare: > >x <- seq(-3, 3) >abs.x.wrong <- if(x < 0) -x else x # Warning message gives some hint >abs.x.right <- ifelse(x < 0, -x, x) > >Hope this helps, > >Michael > >On Sat, May 5, 2012 at 5:09 PM, Santiago Guallar <[hidden email]> wrote: >> Hello, >> >> I have to create a new column from the values of other columns of a data frame. The new column (y$n) is created imposing a condition (using a third variable y$h) that assigns the values of two time variables (y$b and y$timepos). Here's the piece of code to get there (using the attached files): >> >> xact <- read.table("act.lig", sep = ',', col.names=c("ok","time","secs","act")) >> xlig <- read.table("lig.txt", sep = ',', col.names=c("ok","time","secs","lig")) >> w<- merge(xact, xlig, by = c("time" ,"secs"), all = TRUE, sort=F) >> require(reshape) >> z <- cbind(w, colsplit(w$time, split=" ", names=c("date", "clock"))) >> zh<-cbind(z, colsplit(z$clock, split=":", names=c("h","m","s"))) >> zhd<- cbind(zh, colsplit(zh$date, split="/", names=c("d","mo","y"))) >> night <- subset(zhd, zh$lig<6 & zhd$h<9 | zh$lig<6 & zhd$h>21) >> night$timepos<-as.POSIXct(night$time, tz="GMT", format="%d/%m/%y %H:%M:%S") >> a=night$timepos - as.difftime( 1, units="days" ) >> nighta<-cbind(night,a) >> y<- cbind(nighta, b=as.character(a, tz= "GMT", format= "%Y-%m-%d")) >> y$n<-with(y, if (h>=0 & h<9) {b} else {timepos}) ## Missing warnings >> In if (h >= 0 & h < 9) { : >> condition has length > 1 and only the first element will be used >> >> How can I go around this problem and get the new column? >> >> Thank you, >> >> Santi >> ______________________________________________ >> [hidden email] 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. >> > > > > > [hidden email] 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. |
|
It looks like part of your problem is that some of your time/date
variables are stored as factors rather than actual times / dates. Use str() to see which ones and try to convert those. You'll need this format string: format = "%d/%m/%y %H:%M:%S" for the ones that are currently factors. As regards getting POSIXct out of ifelse(), yes -- that seems to be an infelicity, but I'm sure its easily remedied. You just need: as.POSIXct( ifelse( your_code_here) , origin = "1970-01-01") Don't add a format string since that refers to the format of the input (if you're giving a character input), not the output (which is standardized in the definition of POSIXct) Michael On Sun, May 6, 2012 at 5:13 AM, Santiago Guallar <[hidden email]> wrote: > Hi Michael, > Yes, I tried ifelse() before but this function returns a numeric value. When > I try to convert it back to POSIXct I get a <NA>*. If I use if else I get a > POSIXct output although it does not return a correct answer (it only returns > y$timepos even when the condition "h<9" fails to be met). > Attached the outputs you suggested (thanks, by the way, I didn't know > dput()). Hope they go through this time. > > Thank you, > > Santi > > *niga$isnight<- as.POSIXct( niga$nit, tz="GMT", format="%Y-%m-%d %H:%M:%S", > origin="2007-06-19" ) > > > > From: R. Michael Weylandt <[hidden email]> > To: Santiago Guallar <[hidden email]> > Cc: "[hidden email]" <[hidden email]> > Sent: Sunday, May 6, 2012 2:18 AM > Subject: Re: [R] creating a new column assigning values of other columns > > Bahhhh -- far too much work to recreate (and I don't think you sent us > the file "act.lig"): here's a much better route: > > Go to the step immediately before you're in trouble and use dput() on > your data. R will print out a nice plaintext representation that we > can copy and paste and reproduce *exactly* without having to do all > that you show below. > > Incidentally, your warning message suggests you should be using > ifelse() instead of if. > > To compare: > > x <- seq(-3, 3) > abs.x.wrong <- if(x < 0) -x else x # Warning message gives some hint > abs.x.right <- ifelse(x < 0, -x, x) > > Hope this helps, > > Michael > > On Sat, May 5, 2012 at 5:09 PM, Santiago Guallar <[hidden email]> wrote: >> Hello, >> >> I have to create a new column from the values of other columns of a data >> frame. The new column (y$n) is created imposing a condition (using a third >> variable y$h) that assigns the values of two time variables (y$b and >> y$timepos). Here's the piece of code to get there (using the attached >> files): >> >> xact <- read.table("act.lig", sep = ',', >> col.names=c("ok","time","secs","act")) >> xlig <- read.table("lig.txt", sep = ',', >> col.names=c("ok","time","secs","lig")) >> w<- merge(xact, xlig, by = c("time" ,"secs"), all = TRUE, sort=F) >> require(reshape) >> z <- cbind(w, colsplit(w$time, split=" ", names=c("date", "clock"))) >> zh<-cbind(z, colsplit(z$clock, split=":", names=c("h","m","s"))) >> zhd<- cbind(zh, colsplit(zh$date, split="/", names=c("d","mo","y"))) >> night <- subset(zhd, zh$lig<6 & zhd$h<9 | zh$lig<6 & zhd$h>21) >> night$timepos<-as.POSIXct(night$time, tz="GMT", format="%d/%m/%y >> %H:%M:%S") >> a=night$timepos - as.difftime( 1, units="days" ) >> nighta<-cbind(night,a) >> y<- cbind(nighta, b=as.character(a, tz= "GMT", format= "%Y-%m-%d")) >> y$n<-with(y, if (h>=0 & h<9) {b} else {timepos}) ## Missing warnings >> >> In >> if (h >= 0 & h < 9) { : >> >> condition >> has length > 1 and only the first element will be used >> >> How can I go around this problem and get the new column? >> >> Thank you, >> >> Santi >> ______________________________________________ >> [hidden email] 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. >> > > > > ______________________________________________ [hidden email] 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. |
|
It apparently works now. Something weird happens, though: the new column sums 1 extra hour and assigns the day according to its own time. To solve it I subtracted 1 hour to this new column:
niga$night<- as.POSIXct ( ifelse(niga$h>=0 & niga$h<9, niga$a, niga$timepos), origin="1970-01-01") # it works but adds 1 hour (!!!) niga$isnight=niga$night - as.difftime ( 1, units= "hours" ) # subtraction of 1 hour Thank you very much for your time! Santi From: R. Michael Weylandt <[hidden email]> >To: Santiago Guallar <[hidden email]> >Cc: "[hidden email]" <[hidden email]> >Sent: Sunday, May 6, 2012 3:13 PM >Subject: Re: [R] creating a new column assigning values of other columns > >It looks like part of your problem is that some of your time/date >variables are stored as factors rather than actual times / dates. Use >str() to see which ones and try to convert those. You'll need this >format string: format = "%d/%m/%y %H:%M:%S" for the ones that are >currently factors. > >As regards getting POSIXct out of ifelse(), yes -- that seems to be an >infelicity, but I'm sure its easily remedied. You just need: > >as.POSIXct( ifelse( your_code_here) , origin = "1970-01-01") > >Don't add a format string since that refers to the format of the input >(if you're giving a character input), not the output (which is >standardized in the definition of POSIXct) > >Michael > >On Sun, May 6, 2012 at 5:13 AM, Santiago Guallar <[hidden email]> wrote: >> Hi Michael, >> Yes, I tried ifelse() before but this function returns a numeric value. When >> I try to convert it back to POSIXct I get a <NA>*. If I use if else I get a >> POSIXct output although it does not return a correct answer (it only returns >> y$timepos even when the condition "h<9" fails to be met). >> Attached the outputs you suggested (thanks, by the way, I didn't know >> dput()). Hope they go through this time. >> >> Thank you, >> >> Santi >> >> *niga$isnight<- as.POSIXct( niga$nit, tz="GMT", format="%Y-%m-%d %H:%M:%S", >> origin="2007-06-19" ) >> >> >> >> From: R. Michael Weylandt <[hidden email]> >> To: Santiago Guallar <[hidden email]> >> Cc: "[hidden email]" <[hidden email]> >> Sent: Sunday, May 6, 2012 2:18 AM >> Subject: Re: [R] creating a new column assigning values of other columns >> >> Bahhhh -- far too much work to recreate (and I don't think you sent us >> the file "act.lig"): here's a much better route: >> >> Go to the step immediately before you're in trouble and use dput() on >> your data. R will print out a nice plaintext representation that we >> can copy and paste and reproduce *exactly* without having to do all >> that you show below. >> >> Incidentally, your warning message suggests you should be using >> ifelse() instead of if. >> >> To compare: >> >> x <- seq(-3, 3) >> abs.x.wrong <- if(x < 0) -x else x # Warning message gives some hint >> abs.x.right <- ifelse(x < 0, -x, x) >> >> Hope this helps, >> >> Michael >> >> On Sat, May 5, 2012 at 5:09 PM, Santiago Guallar <[hidden email]> wrote: >>> Hello, >>> >>> I have to create a new column from the values of other columns of a data >>> frame. The new column (y$n) is created imposing a condition (using a third >>> variable y$h) that assigns the values of two time variables (y$b and >>> y$timepos). Here's the piece of code to get there (using the attached >>> files): >>> >>> xact <- read.table("act.lig", sep = ',', >>> col.names=c("ok","time","secs","act")) >>> xlig <- read.table("lig.txt", sep = ',', >>> col.names=c("ok","time","secs","lig")) >>> w<- merge(xact, xlig, by = c("time" ,"secs"), all = TRUE, sort=F) >>> require(reshape) >>> z <- cbind(w, colsplit(w$time, split=" ", names=c("date", "clock"))) >>> zh<-cbind(z, colsplit(z$clock, split=":", names=c("h","m","s"))) >>> zhd<- cbind(zh, colsplit(zh$date, split="/", names=c("d","mo","y"))) >>> night <- subset(zhd, zh$lig<6 & zhd$h<9 | zh$lig<6 & zhd$h>21) >>> night$timepos<-as.POSIXct(night$time, tz="GMT", format="%d/%m/%y >>> %H:%M:%S") >>> a=night$timepos - as.difftime( 1, units="days" ) >>> nighta<-cbind(night,a) >>> y<- cbind(nighta, b=as.character(a, tz= "GMT", format= "%Y-%m-%d")) >>> y$n<-with(y, if (h>=0 & h<9) {b} else {timepos}) ## Missing warnings >>> >>> In >>> if (h >= 0 & h < 9) { : >>> >>> condition >>> has length > 1 and only the first element will be used >>> >>> How can I go around this problem and get the new column? >>> >>> Thank you, >>> >>> Santi >>> ______________________________________________ >>> [hidden email] 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. >>> >> >> >> >> > > > ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
