Vijay
2014-Jan-18 04:46 UTC
[R] Performing same steps on multiple dataframes using a loop - vijay
I have three data frames df.1, df.2 and df.3. All have the same structure I.e here is df.1 Name network speed Atlanta_Ga. LTE. 10 Hartford_CT. HSPA. 4 Seattle_WA. LTE. 12 I want to perform a couple of steps on each data frame and store the transformed dataframes as df.1cleaned, df.2cleaned etc...... Here are the steps to perform. 1.Create a new column named state, given by the last two letters of the name field 2. Only keep rows where network is HSPA 3. Multiple the speed column by 1000 So df.1cleaned would look like Name Network. Speed. State Hartford_CT. HSPA. 10000. CT Can someone help me come up with a loop or a function that loops through all dataframes and produces all three dataframes Thanks Vijay Sent from my iPad
arun
2014-Jan-18 07:09 UTC
[R] Performing same steps on multiple dataframes using a loop - vijay
Hi, Try: df.1 <- read.table(text="Name??????????????????? network??????????? speed Atlanta_Ga.????????? LTE.??????????????????? 10 Hartford_CT.????????? HSPA.????????????????? 4 Seattle_WA.????????? LTE.??????????????????? 12",sep="",header=TRUE,stringsAsFactors=FALSE) df.1cleaned <- within(df.1,{ state <-gsub(".*\\_(.*)\\.$","\\1",Name);speed <- speed*1000}) ?subset(df.1cleaned,network=="HSPA.") #check your number in speed column #????????? Name network speed state #2 Hartford_CT.?? HSPA.? 4000??? CT You could use ?lapply() for multiple dataframes df.2 <- df.1 res <- lapply(list(df.1,df.2),function(x) {x1 <- within(x,{state <- gsub(".*\\_(.*)\\.$","\\1",Name); speed <- speed*1000});subset(x1,network=="HSPA.")}) ?names(res) <- paste0(c("df.1","df.2"),"cleaned") res$df.2cleaned A.K. On Saturday, January 18, 2014 1:39 AM, Vijay <vijaychowdhari at gmail.com> wrote: I have three data frames df.1, df.2 and df.3. All have the same structure I.e here is df.1 Name? ? ? ? ? ? ? ? ? ? network? ? ? ? ? ? speed Atlanta_Ga.? ? ? ? ? LTE.? ? ? ? ? ? ? ? ? ? 10 Hartford_CT.? ? ? ? ? HSPA.? ? ? ? ? ? ? ? ? 4 Seattle_WA.? ? ? ? ? LTE.? ? ? ? ? ? ? ? ? ? 12 I want to perform a couple of steps on each data frame and store the transformed dataframes as df.1cleaned, df.2cleaned etc...... Here are the steps to perform. 1.Create a new column named state, given by the last two letters of the name field 2. Only keep rows where network is HSPA 3. Multiple the speed column by 1000 So df.1cleaned would look like Name? ? ? ? ? ? ? ? Network.? ? ? Speed.? ? State Hartford_CT.? ? HSPA.? ? ? ? ? 10000.? ? ? CT Can someone help me come up with a loop or a function that loops through all dataframes and produces all three dataframes Thanks Vijay Sent from my iPad ______________________________________________ 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.
Bert Gunter
2014-Jan-18 07:11 UTC
[R] Performing same steps on multiple dataframes using a loop - vijay
Is this homework? Most of us object to doing homework here. If not, have you gone through any R tutorials? This looks like pretty basic stuff, and one has to wonder if you can't do this, why are you using R at all? Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch On Fri, Jan 17, 2014 at 8:46 PM, Vijay <vijaychowdhari at gmail.com> wrote:> I have three data frames df.1, df.2 and df.3. All have the same structure I.e here is df.1 > > Name network speed > Atlanta_Ga. LTE. 10 > Hartford_CT. HSPA. 4 > Seattle_WA. LTE. 12 > > I want to perform a couple of steps on each data frame and store the transformed dataframes as df.1cleaned, df.2cleaned etc...... > > Here are the steps to perform. > > 1.Create a new column named state, given by the last two letters of the name field > 2. Only keep rows where network is HSPA > 3. Multiple the speed column by 1000 > > So df.1cleaned would look like > Name Network. Speed. State > Hartford_CT. HSPA. 10000. CT > > Can someone help me come up with a loop or a function that loops through all dataframes and produces all three dataframes > > Thanks > Vijay > > > Sent from my iPad > ______________________________________________ > 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.