[R] Performing same steps on multiple dataframes using a loop - vijay

arun smartpink111 at yahoo.com
Sat Jan 18 08:09:44 CET 2014

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")



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


Sent from my iPad
R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list