在“R”中迭代特定列

时间:2015-03-14 03:39:57

标签: r loops for-loop rstudio lapply

更新:谢谢jason和Buckminster - 我使用了您的建议的变体

我使用下面的内容然后调整我的功能/数据再次感谢

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)

使用dput()更新。我想感谢你的回复,并意识到我可以更容易获得帮助。我不得不回去消毒并使数据变小,以便我可以输入()。


我想创建一种优化的方法来迭代我关心的4列,可能使用lappy。

以下是包含6列的几行数据,我只想操作2-5列。此代码段已使用其他代码处理,我认为这与我的问题没有密切关系。

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

这是数据框的deput(),请参阅上面的代码段。我有一个.csv文件dput()但还没有看到如何将其上传到这个问题。

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

在下面的代码中,我手动编辑然后为每列运行(MaximumIn,MaximumOut,AverageIn,AverageOut)。我做的是什么,但是为了简洁起见,不符合R(任何语言)标准。那么下面是基于列的函数候选者?

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

1 个答案:

答案 0 :(得分:1)

这看起来很简单。我没有测试,因为我们没有给出数据,但你应该得到要点。

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)