[R] creating a new column assigning values of other columns
Santiago Guallar
sguallar at yahoo.com
Sat May 5 23:09:36 CEST 2012
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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: lig.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120505/437cc0b6/attachment.txt>
More information about the R-help
mailing list