通过 sapply 应用多个功能 [英] Applying multiple function via sapply
问题描述
我正在尝试复制在 sapply
中应用多个函数的解决方案 R-Bloggers 但我无法让它以所需的方式工作.我正在处理一个简单的数据集,类似于下面生成的数据集:
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:
- 遍历前两列中的所有值(Var1 和 Var2),并通过
gsub
执行简单的替换. - 理想情况下,我希望避免定义单独的函数,如 linked 发布并保留所有内容in
sapply
语法 - 我不想要嵌套循环
- Go through all the values in first two columns (Var1 and Var2) and perform simple replacements via
gsub
. - Ideally, I would like to avoid defining a separate function, as discussed in the linked post and keep everything within the
sapply
syntax - 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屋!