Can I use gsub() on each element of a data frame?

前端 未结 2 475
醉话见心
醉话见心 2020-12-16 17:27

After importing a table from Wikipedia, I have a list of values of the following form:

    > tbl[2:6]
    $`Internet
    Explorer`
     [1] \"30.71%\" \"3         


        
相关标签:
2条回答
  • 2020-12-16 18:05

    Well I think you could do it the following way, but I don't know if it is better or cleaner than yours :

    df <- data.frame(tbl)
    df[,-1] <- as.numeric(gsub("%", "", as.matrix(df[,-1])))
    

    Which gives :

    R> head(df)
                Date Internet.Explorer Chrome Firefox Safari Opera Mobile
    1   January 2013             30.71  36.52   21.42   8.29  1.19  14.13
    2  December 2012             30.78  36.42   21.89   7.92  1.26  14.55
    3  November 2012             31.23  35.72   22.37   7.83  1.39  13.08
    4   October 2012             32.08  34.77   22.32   7.81  1.63  12.30
    5 September 2012             32.70  34.21   22.40   7.70  1.61  12.03
    6    August 2012             32.85  33.59   22.85   7.39  1.63  11.78
    R> sapply(df, class)
                 Date Internet.Explorer            Chrome           Firefox 
             "factor"         "numeric"         "numeric"         "numeric" 
               Safari             Opera            Mobile 
            "numeric"         "numeric"         "numeric" 
    
    0 讨论(0)
  • 2020-12-16 18:19

    Like juba I'm uncertain if this way is "better or cleaner" but...to act on all elements of a data frame, you can use apply:

    # start with data frame, not list
    url <- "http://en.wikipedia.org/wiki/Usage_share_of_web_browsers"
    # Get the eleventh table.
    tbl <- readHTMLTable(url, which = 11, stringsAsFactors = F)
    
    # use apply on the non-date columns
    tbl[, 2:7] <- apply(tbl[, 2:7], 2, function(x) as.numeric(gsub("%", "", x)))
    
    0 讨论(0)
提交回复
热议问题