constructing a function using colnames as variables

被刻印的时光 ゝ 提交于 2019-12-06 12:22:06

Let's assume you're starting with something like this as your data.frame:

mydf <- data.frame(
  v1 = c("A, B, B", "A, C,D"), 
  v2 = c("E, F", " G,H , E, I"), 
  v3 = c("J,K,L,M", "N, J, L, M, K"))

mydf
#        v1          v2            v3
# 1 A, B, B        E, F       J,K,L,M
# 2  A, C,D  G,H , E, I N, J, L, M, K

One way you can define your function would be like the following. I've stuck to base functions, but you can use "stringr" if you prefer.

myFun <- function(instring) {
  if (!is.character(instring)) instring <- as.character(instring)
  unique(trimws(unlist(strsplit(instring, ",", fixed = TRUE))))
}

The first line just checks to see if the input is a character string or not. Often, in data.frames, data is read in with stringsAsFactors = TRUE by default, so you need to perform that conversion first. The second line does the splitting and trimming. I've added a fixed = TRUE in there for efficiency.

Once you have such a function, you can easily apply it using apply (for a data.frame or a matrix, either by row or by column) or using lapply (for a list or a data.frame (which would be by column)).

## If `mydf` is a data.frame, and you want to go by columns
lapply(mydf, myFun) 
# $v1
# [1] "A" "B" "C" "D"
# 
# $v2
# [1] "E" "F" "G" "H" "I"
# 
# $v3
# [1] "J" "K" "L" "M" "N"

## `apply` can be used too. Second argument specifies whether by row or column
apply(mydf, 1, myFun)
apply(mydf, 2, myFun)

If, on the other hand, you are looking to create a function that accepts the input dataset name and the (bare, unquoted) column, you can write your function like this:

myOtherFun <- function(indf, col) {
  col <- deparse(substitute(col))
  unique(trimws(unlist(strsplit(as.character(indf[, col]), ",", TRUE))))
}

The first line captures the bare column name as a character string so that it could be used in the typical my_data[, "col_wanted"] form.

Here's the function in use:

myOtherFun(mydf, v2)
# [1] "E" "F" "G" "H" "I"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!