Christofer Bogaso
2012-Dec-10 20:50 UTC
[R] Can somebody suggest how to achieve following data manipulation?
Dear all, Let say I have following data: RawData <- matrix(1:101, nr = 1); colnames(RawData) <- c("ASD", as.character(as.yearmon(seq(as.Date("2012-03-01"), length.out = 100, by = "1 month")))); rownames(RawData) <- "XYZ" CutOffDate <- as.Date("2012-09-01") NewDateSeries <- as.character(as.yearmon(seq(CutOffDate, to = as.Date("2025-01-01"), by = "1 month"))) ResultMat <- matrix(NA, 1, length(NewDateSeries)) colnames(ResultMat) <- NewDateSeries rownames(ResultMat) <- "Result" RawData ResultMat Now I need to pass the elements of 'RawData' to 'ResultMat' in following way: Consider the column 'Sep 2012' of 'ResultMat' (i.e. the first column). The element for this column will be sum of all elements of columns less than or equal to 'Sep 2012' of 'RawData' and including the column 'ASD'. Therefore, the column 'Sep 2012' of 'ResultMat' will have the element as "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36". And remaining columns of 'ResultMat'will get the data from the same column names of 'RawData'. Therefore the 2nd column of 'ResultMat' will have the element as 9 and so on. I need to adopt above filling policy for arbitrary 'CutOffDate'. Ofcourse this can be implemented using a 'for' loop. However I believe there is some better R-way to do that. Can somebody help me to achieve that? Thanks and regards,
arun
2012-Dec-10 21:24 UTC
[R] Can somebody suggest how to achieve following data manipulation?
Hi, It's not very clear. Here, the dimensions are different. dim(RawData) #[1]?? 1 101 ?dim(ResultMat) #[1]?? 1 149 ResultMat[,1]<-sum(RawData[RawData<=RawData[8]]) ResultMat[,2:94]<-RawData[RawData>=RawData[9]] A.K. ----- Original Message ----- From: Christofer Bogaso <bogaso.christofer at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, December 10, 2012 3:50 PM Subject: [R] Can somebody suggest how to achieve following data manipulation? Dear all, Let say I have following data: RawData <- matrix(1:101, nr = 1); colnames(RawData) <- c("ASD", as.character(as.yearmon(seq(as.Date("2012-03-01"), length.out = 100, by = "1 month")))); rownames(RawData) <- "XYZ" CutOffDate <- as.Date("2012-09-01") NewDateSeries <- as.character(as.yearmon(seq(CutOffDate, to = as.Date("2025-01-01"), by = "1 month"))) ResultMat <- matrix(NA, 1, length(NewDateSeries)) colnames(ResultMat) <- NewDateSeries rownames(ResultMat) <- "Result" RawData ResultMat Now I need to pass the elements of 'RawData' to 'ResultMat' in following way: Consider the column 'Sep 2012' of 'ResultMat' (i.e. the first column). The element for this column will be sum of all elements of columns less than or equal to 'Sep 2012' of 'RawData' and including the column 'ASD'. Therefore, the column 'Sep 2012' of 'ResultMat' will have the element as "1? ? +? ? 2? ? +? ? 3? ? +? ? 4? + 5? ? +? 6? ? +? 7? ? +? 8 = 36". And remaining columns of 'ResultMat'will get the data from the same column names of 'RawData'. Therefore the 2nd column of 'ResultMat' will have the element as 9 and so on. I need to adopt above filling policy for arbitrary 'CutOffDate'. Ofcourse this can be implemented using a 'for' loop. However I believe there is some better R-way to do that. Can somebody help me to achieve that? Thanks and regards, ______________________________________________ R-help at 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.