Folks: Good day. Please see the code below. three_wk_out is a dataframe with columns wk1 through wk209. I want to change the format of the columns. I am trying the code below but it does not work. I need $week in the for loop interpreted as wk1, wk2, etc. Could you please help? Thanks. Satish R code below week_list <- paste("wk",c(1:209),sep="") for (week in week_list) { three_wk_out$week <- as.numeric(three_wk_out$week) }
Tena koe Satish Try using three_wk_out[,week] <- as.numeric(tree_wk_out[,week]) HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Vadlamani, > Satish {FLNA} > Sent: Monday, 8 February 2010 1:51 p.m. > To: r-help at r-project.org > Subject: [R] dataframe question > > Folks: > Good day. Please see the code below. three_wk_out is a > dataframe with columns wk1 through wk209. I want to change > the format of the columns. I am trying the code below but it > does not work. I need $week in the for loop interpreted as > wk1, wk2, etc. Could you please help? Thanks. > Satish > > R code below > week_list <- paste("wk",c(1:209),sep="") for (week in week_list) { > three_wk_out$week <- as.numeric(three_wk_out$week) } > > ______________________________________________ > 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. >
On Feb 7, 2010, at 7:51 PM, Vadlamani, Satish {FLNA} wrote:> Folks: > Good day. Please see the code below. three_wk_out is a dataframe > with columns wk1 through wk209. I want to change the format of the > columns. I am trying the code below but it does not work. I need > $week in the for loop interpreted as wk1, wk2, etc. Could you please > help? Thanks. > Satish > > R code below > week_list <- paste("wk",c(1:209),sep="")Or more "functionally": three_wk_out <- as.data.frame( lapply(three_wk_out, some_function) ) E.g.: > df a b c x 1 1 0 0 1 2 2 3 2 4 3 1 2 1 5 4 2 0 3 2 > df <- as.data.frame(lapply(df, "^", 2)) > df a b c x 1 1 0 0 1 2 16 81 16 256 3 1 16 1 625 4 16 0 81 16> for (week in week_list) > { > three_wk_out$week <- as.numeric(three_wk_out$week) > } > > ______________________________________________ > 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.
On Feb 7, 2010, at 8:14 PM, David Winsemius wrote:> > On Feb 7, 2010, at 7:51 PM, Vadlamani, Satish {FLNA} wrote: > >> Folks: >> Good day. Please see the code below. three_wk_out is a dataframe >> with columns wk1 through wk209. I want to change the format of the >> columns. I am trying the code below but it does not work. I need >> $week in the for loop interpreted as wk1, wk2, etc. Could you >> please help? Thanks. >> Satish >> >> R code below >> week_list <- paste("wk",c(1:209),sep="") > > > Or more "functionally": > > three_wk_out <- as.data.frame( lapply(three_wk_out, some_function) )Or if you wanted to just change the particular columns that matched the "wk" pattern: idx <- grep("wk", names(three_wk_out)) three_wk_out[, idx ] <- apply( three_wk_out[, idx ], 2, as.numeric) (I probably should have used apply( ___ , 2, fn) in the prior effort rather than coercing a list back to a dataframe.)> > E.g.: > >> a b c x > 1 1 0 0 1 > 2 2 3 2 4 > 3 1 2 1 5 > 4 2 0 3 2 > > > df <- as.data.frame(lapply(df, "^", 2)) > > df > a b c x > 1 1 0 0 1 > 2 16 81 16 256 > 3 1 16 1 625 > 4 16 0 81 16 > > >> for (week in week_list) >> { >> three_wk_out$week <- as.numeric(three_wk_out$week) >> } >> >> ______________________________________________ >> 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. > > ______________________________________________ > 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.