Applying multiple function via sapply

一个人想着一个人 提交于 2021-01-28 03:55:24

问题


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

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. 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

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.

Edit

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)

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.


回答1:


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



回答2:


Here is a start of a solution for you, I think you're capable of extending it yourself. There's probably more elegant approaches available, but I don't see them atm.

crs_mat[,1:2] <- sapply(crs_mat[,1:2], function(x) {
  # Replace first phrase
  step1 <- gsub("mpg","MPG",x)
  # Replace second phrase. Note that this operates on a modified dataframe. 
  step2 <- gsub("gear", "GeArr",step1)
  # Ideally, perform other changes
  return(step2)

  #or one nested line, not practical if more needs to be done
  #return(gsub("gear", "GeArr",gsub("mpg","MPG",x)))
})


来源:https://stackoverflow.com/questions/33479208/applying-multiple-function-via-sapply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!