【问题标题】:Using magrittr with the (*)apply functions - differing output将 magrittr 与 (*)apply 函数一起使用 - 不同的输出
【发布时间】:2016-01-23 05:20:00
【问题描述】:

我将文件读入包含一列字符的 data.frame,并使用 sapply 按元素应用一些函数。我对使用 magrittr 包中的 pipes 很感兴趣,我想知道这样做的正确和最佳方法是什么。我已经尝试了一些变体,虽然它有效(我相信!),但两种方法的输出确实不同。

我省略了 sapply 本身的占位符,作为下一个函数的 %>% pipes into the first parameter

原始代码

## Load in a CSV file as a dataframe
   y <- read.csv(file = file_name, header = TRUE, sep = "\n", quote = "", row.names = NULL, stringsAsFactors = FALSE)
## Perform so operations
y$transformed1 <- sapply(y, FUN = function(x) gsub("&amp;", "&", x))
y$transformed2 <- sapply(y$transformed1, FUN = function(x) gsub(pattern = "http\\S+\\s*", replacement = "", x)) 
y$transformed3 <- sapply(y$transformed2, FUN = function(x) gsub("[^[:alpha:][:space:]&\']", "", x)) 
y$transformed4 <- sapply(y$transformed3, FUN = function(x) stripWhitespace(x)) 
y$transformed5 <- sapply(y$transformed4, FUN = function(x) gsub("^ ", "", x))
y$transformed6 <- sapply(y$transformed5, FUN = function(x) gsub(" $", "", x))

这对我来说非常有用,在y$transformed6 中返回满足我需要的干净结果。

使用magrittr

以下代码运行良好,通过目视检查,结果看起来相同,如下面的head 函数比较所示。

in_file <- y   ## from above

out_file <-  sapply(in_file, function(x) gsub("&amp;", "&", x)) %>%
                    gsub("http\\S+\\s*", "", .) %>%
                    gsub("[^[:alpha:][:space:]&\']", "", .) %>%
                    stripWhitespace() %>%
                    gsub("^ ", "", .) %>%
                    gsub(" $", "", .)

在这里,您可以看到每个输出的 str()head() 函数的返回值。在它们上使用identical 函数不出所料地返回FALSE

## First method

> str(y$transformed6)                                
 chr [1:14158] "ExchangeNews Direct S&P Dow Jones Ind

> head(y$transformed6)                               
[1] "ExchangeNews Direct S&P Dow Jones Indices Announ
[2] "Svelte Medical Systems Raises M for HeartSurgery
[3] "Dow Jones industrial average tumbles below on bu
[4] "Dow approaches record high as Fed meeting begins
[5] "Money How the Dow Jones industrial average did T
[6] "Just another day at dowjones brewing up exciting

-----------------------------------------------------

## Using magittr

> str(out_file)                                      
 chr [1:14158, 1] "ExchangeNews Direct S&P Dow Jones 
 - attr(*, "dimnames")=List of 2                     
  ..$ : NULL                                         
  ..$ : chr "text"                                   

> head(out_file)                                     
     text                                            
[1,] "ExchangeNews Direct S&P Dow Jones Indices Annou
[2,] "Svelte Medical Systems Raises M for HeartSurger
[3,] "Dow Jones industrial average tumbles below on b
[4,] "Dow approaches record high as Fed meeting begin
[5,] "Money How the Dow Jones industrial average did 
[6,] "Just another day at dowjones brewing up excitin

差异究竟来自哪里?我做错了什么还是只是使用magittr 的产物?

【问题讨论】:

  • 通过查看 dplyr 和 dplyr::mutate 似乎你可能会得到最好的服务
  • 你这里只转换一列?你为什么要使用sapplygsub 至少在您使用它的方式上是一个很好的矢量化函数。在管道版本中,您只是 sapply-ing 第一步,而不是像您在第一个场景中所做的其他步骤。
  • 我确实这么认为,但如果我把 sapply 留在外面,那么我将返回一个向量,而不是一个数据框 - 我需要一个数据框。

标签: r


【解决方案1】:

您应该将管道放在sapply 内,而不是之后

out_file <-  sapply(in_file, function(x) {gsub("&amp;", "&", x) %>%
    gsub("http\\S+\\s*", "", .) %>%
    gsub("[^[:alpha:][:space:]&\']", "", .) %>%
    stripWhitespace() %>%
    gsub("^ ", "", .) %>%
    gsub(" $", "", .)
})

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2022-11-14
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多