JohnP
JohnP

Reputation: 65

In "R" iterate over specific columns

Update: Thanks jason and Buckminster - I used a variation of your suggestions

I used the below then adjusted for my function/data Again thanks

myFun<-function(x) {

        myDF$multiple[grep(" Mbps",myDF[,x])] <- 1000000
        myDF[,x] <- gsub(" Mbps","",myDF[,x]) 

        myDF$multiple[grep(" Kbps",myDF[,x])] <- 1000
        myDF[,x] <- gsub(" Kbps","",myDF[,x]) 

        myDF$multiple[grep(" bps",myDF[,x])] <- 1 
        myDF[,x] <- gsub(" bps","",myDF[,x])

        myDF[,x] <- as.numeric(myDF[,x]) * myDF$multiple

}

cols<-c('MaximumIn','MaximumOut','AverageIn','AverageOut')
myDF[ ,2:5]<-lapply(cols,myFun)

UPDATED with dput(). I want to thank you for replying and realize that I could have made it easier to get help. I had to go back and sanitize and make the data small so that I could dput().


I'd like to create an optimized way to iterate over the 4 columns I care about, possibly using lappy.

Below are a few lines of my data with 6 columns, I only want to manipulate columns 2-5. This snippet has been processed with other code that i don't think is germane to my question.

Host      MaximumIn     MaximumOut  AverageIn   AverageOut  Site Name   Date
device1   30.63 Kbps    0 bps       24.60 Kbps  0 bps       SiteA       3/7/15
device12  1.13 Mbps     24.89 Kbps  21.76 Kbps  461 bps     SiteA       3/8/15
device1   698.44 Kbps   37.71 Kbps  17.49 Kbps  3.37 Kbps   SiteB       3/7/15

Here is a deput() of the dataframe see snippet above. I have a .csv file dput() but don't yet see how to upload that to this question.

structure(list(Host = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
7L, 8L, 9L, 10L), .Label = c("DeviceS1", "DeviceS2", "DeviceS3", 
"DeviceS4", "DeviceS5", "deviceS2a", "deviceS2b", "devices5a", 
"devices5b", "devices5c"), class = "factor"), MaximumIn = structure(c(5L, 
2L, 3L, 1L, 4L, 6L, 7L, 8L, 11L, 10L, 9L), .Label = c("121.02 Kbps", 
"27.11 Kbps", "39.08 Kbps", "62.22 Kbps", "698.44 Kbps", "1.21 Mbps", 
"3.52 Mbps", "606.44 Kbps", "16.19 Mbps", "34.04 Mbps", "34.21 Mbps"
), class = "factor"), MaximumOut = structure(c(5L, 1L, 2L, 4L, 
3L, 6L, 7L, 8L, 9L, 11L, 10L), .Label = c("0 bps", "10.58 Kbps", 
"18.94 Kbps", "33.26 Kbps", "37.71 Kbps", "4.08 Mbps", "405.38 Kbps", 
"930.44 Kbps", "15.35 Mbps", "192.88 Kbps", "2.98 Mbps"), class = "factor"), 
    AverageIn = structure(c(4L, 2L, 1L, 5L, 3L, 8L, 7L, 6L, 10L, 
    9L, 11L), .Label = c("10.83 Kbps", "24.57 Kbps", "3.87 Kbps", 
    "30.36 Kbps", "9.76 Kbps", "170.21 Kbps", "210.04 Kbps", 
    "312.39 Kbps", "20.08 Mbps", "21.60 Mbps", "5.95 Mbps"), class =     "factor"), 
    AverageOut = structure(c(5L, 1L, 4L, 3L, 2L, 8L, 7L, 6L, 
    11L, 10L, 9L), .Label = c("0 bps", "1.54 Kbps", "2.28 Kbps", 
    "5.01 Kbps", "5.08 Kbps", "124.78 Kbps", "26.42 Kbps", "599.09 Kbps", 
    "21.38 Kbps", "576.77 Kbps", "6.16 Mbps"), class = "factor"), 
    `Site Name` = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    3L, 3L, 3L), .Label = c("site1", "site2", "site5"), class = "factor"), 
    Date = structure(c(16475, 16475, 16475, 16475, 16475, 16476, 
    16476, 16476, 16476, 16476, 16476), class = "Date")), .Names = c("Host", 
"MaximumIn", "MaximumOut", "AverageIn", "AverageOut", "Site Name", 
"Date"), row.names = c(NA, 11L), class = "data.frame")

In the code below I manually edit then run for each column (MaximumIn, MaximumOut, AverageIn, AverageOut). What I do works but isn't up to the R (any language) standards for brevity. So the below is a candidate for function based on columns?

myDF$multiple <- 1

myDF$multiple[grep(" Mbps",myDF$MaximumOut)] <- 1000000
myDF$MaximumOut <- gsub(" Mbps","",myDF$MaximumOut) 

myDF$multiple[grep(" Kbps",myDF$MaximumOut)] <- 1000
myDF$MaximumOut <- gsub(" Kbps","",myDF$MaximumOut) 

myDF$multiple[grep(" bps",myDF$MaximumOut)] <- 1 
myDF$MaximumOut <- gsub(" bps","",myDF$MaximumOut)

myDF$MaximumOut <- as.numeric(myDF$MaximumOut) * myDF$multiple

Upvotes: 0

Views: 797

Answers (1)

Jason
Jason

Reputation: 1569

This seems simple enough. I didn't test as we weren't given data but you should get the gist.

myFun<-function(col) {

    myDF$multiple[grep(" Mbps",myDF[,col])] <- 1000000
    myDF[,col] <- gsub(" Mbps","",myDF[,col]) 

    myDF$multiple[grep(" Kbps",myDF[,col])] <- 1000
    myDF[,col] <- gsub(" Kbps","",myDF[,col]) 

    myDF$multiple[grep(" bps",myDF[,col])] <- 1 
    myDF[,col] <- gsub(" bps","",myDF[,col])

    myDF[,col] <- as.numeric(myDF[,col]) * myDF$multiple

}

cols<-c('MaximumOut','AverageIn','AverageOut','MaximumIn')
lapply(cols,myFun)

Upvotes: 1

Related Questions