[R] problem converting character to dates
Hi all, I've searched this problem and still I can't understand my results, so here goes: I have some time series I've imported from excel with the dates in text format from excel. Data was imported with RODBC, sqlQuery() function. I have these dates: >adates [1] "01/2008" "02/2008" "03/2008" "04/2008" "05/2008" "06/2008" "07/2008" [8] "08/2008" "09/2008" "10/2008" "11/2008" "12/2008" "13/2008" "14/2008" I want the format week/year, so I do: >as.Date(adates,format=c("%W/%y")) and get [1] "2020-05-12" "2020-05-12" "2020-05-12" "2020-05-12" "2020-05-12" [6] "2020-05-12" "2020-05-12" "2020-05-12" "2020-05-12" "2020-05-12" everything is equal to this: 2020-this month-today if I use strptime(dates, "%W/%y") it's the same. Can you explain why this happens and how to solve it? Thanks in advance Assu -- View this message in context: http://r.789695.n4.nabble.com/problem-converting-character-to-dates-tp3517918p3517918.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] problem converting character to dates
Thanks Uwe, it works! I just have a problem at week 53 which gives me an NA. I guess, this week 53 has to do with the data type set at the database design and collection of data. Depends on the week/year kind of counting. -- View this message in context: http://r.789695.n4.nabble.com/problem-converting-character-to-dates-tp3517918p3519722.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] problem converting character to dates
I have imported the data fram Excel and it comes like this: Calendar Year/Month 01.2008 01.2008 01.2008 01.2008 01.2008 02.2008 02.2008 Calendar year / week01.2008 02.2008 03.2008 04.2008 05.2008 05.2008 06.2008 There are repeats in the weeks both belonging to two months. It's the same at the end of the year. So, I think it was to do with the data acquisition, probably, and how it is saved in the database. Thanks for your help and interest. -- View this message in context: http://r.789695.n4.nabble.com/problem-converting-character-to-dates-tp3517918p3524837.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] conditional rowsums in sapply
Hi all I have a data frame with duplicate columns and i want to remove duplicates by adding rows in each group of duplicates, but have lots of NA's. Data: dfrm <- data.frame(a = 1:4, b= 1:4, cc= 1:4, dd=1:10, ee=1:4) names(dfrm) <- c("a", "a", "b", "b", "b") dfrm[3,2:3]<-NA dfrm a a b b b 1 1 1 1 1 1 2 2 2 2 2 2 3 NA NA NA 3 3 4 4 4 4 4 4 I did: sapply(unique(names(dfrm)),function(x){ rowSums(dfrm[ ,grep(x, names(dfrm)),drop=FALSE])}) which works. However, I want rowSums conditional: 1) if there is at least one value non NA in a row of each group of duplicates, apply rowSums to get the value independently of the existence of other NA's in the group row. 2) if all values in a row of duplicates are NA, I get NA In my data dfrm I would get a b 12 3 24 6 3 NA 6 48 12 Can't use na.rm=TRUE or FALSE. I tried: sapply(unique(names(dfrm)),function(x) ifelse(any(!is.na(dfrm[ ,grep(x, names(dfrm))])), rowSums(dfrm[ ,grep(x, names(dfrm)),drop=FALSE],na.rm=TRUE),NA)) and it doesn't work. Can someone please help me? Thanks in advance. -- View this message in context: http://r.789695.n4.nabble.com/conditional-rowsums-in-sapply-tp3526332p3526332.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] conditional rowsums in sapply
Hello David, I'm not sure I get your question; if you replied to SO, I had not seen it. I posted there today. I'm reading it now! I chose not to receive replies in my email from SO but I must change that. Thank you for making me see that! Sorry, I'm new to R-help and, especially, SO. Thanks for your reply! My attempt was working only on the first row. -- View this message in context: http://r.789695.n4.nabble.com/conditional-rowsums-in-sapply-tp3526332p3527242.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.