[R] preserving date formats in functions
Hi there! I have two date columns in a dataframe I need to selectively collapse based on missing values and which date comes first (imported from an text file). This is what I did: RHSSP$CT<- as.POSIXct(RHSSP$CT, format='%m/%d/%y %H:%M') RHSSP$MRI <- as.POSIXct(RHSSP$MRI, format='%m/%d/%y %H:%M') RHSSP$Scan <- ifelse(is.na(RHSSP$MRI),RHSSP$CT,ifelse(is.na(RHSSP$CT),RHSSP$MRI,ifelse(RHSSP$CT>RHSSP$MRI,RHSSP$MRI,RHSSP$CT))) RHSSP$CT and RHSSP$MRI remain properly formatted (e.g.: "2011-06-21 22:31:00 PDT") BUT RHSSP$Scan does not (e.g.: 1294076700) I can't figure out how to maintain the proper format during the replacement. Looks like the replacement inherits the ifelse comparison format. Any suggestions? Many thanks Jon -- View this message in context: http://r.789695.n4.nabble.com/preserving-date-formats-in-functions-tp4632435.html Sent from the R help mailing list archive at Nabble.com. __ 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.
[R] subset columns from list with variable substitution
Hi there, I would like to use a list variable to select columns in a subset from a parent table: I have a data frame "table" with column headers a,b,c,d,e,x,y,z and list variables list1=c("a","b","c","d") list2=c("a","b","x",y","z") namelist=c("peter","paul","mary","jane") group1=c("peter","paul") group2=c("mary","jane") I would like to subset "table" based on the list variable in a for loop: for (i %in% namelist){ if (i %in% group1){table2<-subset(table, select=list1)} else {{table2<-subset(table, select=list2)} } the "select=list1" syntax does not work. What would be the correct way to do this? Many Thanks Jon -- View this message in context: http://r.789695.n4.nabble.com/subset-columns-from-list-with-variable-substitution-tp4631374.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] subset columns from list with variable substitution
Thanks Don but table[,list1] did not work either: Error in `[.data.frame`(table, , list1) : undefined columns selected. I'm guessing my list (list1) is not structured right? Displaying it has no commas, so the whole list may be taken as a single variable rather than a sequence of variables? I've tried various ways of reformatting (c(), as.list(), etc), but no go. also "i in namelist" does not work while "i %in% namelist" does. I don't really reference "i" in any function, only using it in the conditional. Any other suggestions? Thanks Jon -- View this message in context: http://r.789695.n4.nabble.com/subset-columns-from-list-with-variable-substitution-tp4631374p4631394.html Sent from the R help mailing list archive at Nabble.com. __ 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.