通过sapply应用多项功能 [英] Applying multiple function via sapply

查看:43
本文介绍了通过sapply应用多项功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制发布在

I'm trying to replicate solution on applying multiple functions in sapply posted on R-Bloggers but I can't get it to work in the desired manner. I'm working with a simple data set, similar to the one generated below:

require(datasets)
crs_mat <- cor(mtcars)

# Triangle function
get_upper_tri <- function(cormat){
  cormat[lower.tri(cormat)] <- NA
  return(cormat)
}

require(reshape2)
crs_mat <- melt(get_upper_tri(crs_mat))

我想替换 Var1 Var2 列中的一些文本值.下面的错误语法说明了我正在尝试实现的目标:

I would like to replace some text values across columns Var1 and Var2. The erroneous syntax below illustrates what I am trying to achieve:

crs_mat[,1:2] <- sapply(crs_mat[,1:2], function(x) {
 # Replace first phrase
 gsub("mpg","MPG",x), 
 # Replace second phrase
  gsub("gear", "GeArr",x)
 # Ideally, perform other changes
})

自然,该代码在语法上不正确并且失败.总结一下,我想做以下事情:

Naturally, the code is not syntactically correct and fails. To summarise, I would like to do the following:

  1. 遍历前两列(Var1和Var2)中的所有值,然后通过 gsub 执行简单的替换.
  2. 理想情况下,我希望避免定义单独的函数,如链接发布并保持所有内容 sapply 语法
  3. 我不要嵌套循环
  1. Go through all the values in first two columns (Var1 and Var2) and perform simple replacements via gsub.
  2. Ideally, I would like to avoid defining a separate function, as discussed in the linked post and keep everything within the sapply syntax
  3. I don't want a nested loop


我看了此处此处,但是,如果可能的话,我想避免使用 plyr .我也有兴趣替换列值而不是创建新列,并且我想避免指定任何列名.在使用现有数据框时,使用列号更为方便.


I had a look at the broadly similar subject discussed here and here but, if possible, I would like to avoid making use of plyr. I'm also interested in replacing the column values not in creating new columns and I would like to avoid specifying any column names. While working with my existing data frame it is more convenient for me to use column numbers.

以下非常有用的评论,可以在下面的解决方案中总结我要实现的目标:

Following very useful comments, what I'm trying to achieve can be summarised in the solution below:

fun.clean.columns <- function(x, str_width = 15) {
  # Make character
  x <- as.character(x)
  # Replace various phrases
  x <- gsub("perc85","something else", x)
  x <- gsub("again", x)
  x <- gsub("more","even more", x)
  x <- gsub("abc","ohmg", x)
  # Clean spaces
  x <- trimws(x)
  # Wrap strings
  x <- str_wrap(x, width = str_width)
  # Return object
  return(x)
}
mean_data[,1:2] <- sapply(mean_data[,1:2], fun.clean.columns)

我在我的 global.env 中不需要此功能,因此我可以在此之后运行 rm ,但是甚至更好的解决方案也将涉及 squeezing apply 语法中.

I don't need this function in my global.env so I can run rm after this but even nicer solution would involve squeezing this within the apply syntax.

推荐答案

我们可以使用 library(qdap)中的 mgsub 替换多个模式.在这里,我使用 lapply 循环第一和第二列,并将结果分配回 crs_mat [,1:2] .请注意,我使用的是 lapply 而不是 sapply ,因为 lapply 会使结构保持完整

We can use mgsub from library(qdap) to replace multiple patterns. Here, I am looping the first and second column using lapply and assign the results back to the crs_mat[,1:2]. Note that I am using lapply instead of sapply as lapply keeps the structure intact

library(qdap)
crs_mat[,1:2] <- lapply(crs_mat[,1:2], mgsub, 
   pattern=c('mpg', 'gear'), replacement=c('MPG', 'GeArr'))

这篇关于通过sapply应用多项功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆