在函数中将对象名称转换为字符串

Convert object name to string in function

我有 listdata.frames。我想使用 lapply 将每个 data.frame 发送到 function。在 function 中,我想检查 data.framename 是否包含特定的 string。如果有问题的 string 存在,我想执行一系列操作。否则我想执行一系列不同的操作。我不知道如何检查 function.

中是否存在有问题的 string

我想使用基础 R。这似乎是一个可能的解决方案,但我无法让它工作:

In R, how to get an object's name after it is sent to a function?

这是一个示例 list,下面是一个示例 function

matrix.apple1 <- read.table(text = '
     X3   X4   X5
      1    1    1
      1    1    1
', header = TRUE)
matrix.apple2 <- read.table(text = '
     X3   X4   X5
      1    1    1
      2    2    2
', header = TRUE)
matrix.orange1 <- read.table(text = '
     X3   X4   X5
     10   10   10
     20   20   20
', header = TRUE)
my.list <- list(matrix.apple1  = matrix.apple1,
                matrix.orange1 = matrix.orange1,
                matrix.apple2  = matrix.apple2)

这个操作可以检查每个对象name是否包含stringapples 但我不确定如何在下面的 function 中使用此信息。

grepl('apple', names(my.list), fixed = TRUE)
#[1]  TRUE FALSE  TRUE

这是一个例子function。基于数小时的搜索和反复试验,我也许应该使用 deparse(substitute(x)) 但到目前为止它只有 returns x 或类似的东西。

table.function <- function(x) {

     # The three object names are:
     # 'matrix.apple1', 'matrix.orange1' and 'matrix.apple2'
     myObjectName <- deparse(substitute(x))
     print(myObjectName)

     # perform a trivial example operation on a data.frame
     my.table <- table(as.matrix(x))

     # Test whether an object name contains the string 'apple'
     contains.apple <- grep('apple', myObjectName, fixed = TRUE)

     # Use the result of the above test to perform a trivial example operation.
     # With my code 'my.binomial' is always given the value of 0 even though
     # 'apple' appears in the name of two of the data.frames.
     my.binomial <- ifelse(contains.apple == 1, 1, 0)

     return(list(my.table = my.table, my.binomial = my.binomial))
        
}

table.function.output <- lapply(my.list, function(x) table.function(x))

这些是 print(myObjectName) 的结果:

#[1] "x"
#[1] "x"
#[1] "x"

table.function.output

以下是 table.function 的其余结果,表明 my.binomial 总是 0my.binomial 的第一个和第三个值应该是 1 因为第一个和第三个 data.framesnames 包含 string apple.

# $matrix.apple1
# $matrix.apple1$my.table
# 1 
# 6 
# $matrix.apple1$my.binomial
# logical(0)
# 
# $matrix.orange1
# $matrix.orange1$my.table
# 10 20 
#  3  3 
# $matrix.orange1$my.binomial
# logical(0)
# 
# $matrix.apple2
# $matrix.apple2$my.table
# 1 2 
# 3 3 
# $matrix.apple2$my.binomial
# logical(0)

您可以重新设计函数以改为使用列表名称:

table_function <- function(myObjectName) {
  # The three object names are:
  # 'matrix.apple1', 'matrix.orange1' and 'matrix.apple2'
  myObject <- get(myObjectName)
  
  print(myObjectName)
  
  # perform a trivial example operation on a data.frame
  my.table <- table(as.matrix(myObject))
  
  # Test whether an object name contains the string 'apple'
  contains.apple <- grep('apple', myObjectName, fixed = TRUE)
  
  # Use the result of the above test to perform a trivial example operation.
  # With my code 'my.binomial' is always given the value of 0 even though
  # 'apple' appears in the name of two of the data.frames.
  my.binomial <- +(contains.apple == 1)
  
  return(list(my.table = my.table, my.binomial = my.binomial))
  
}


lapply(names(my.list), table_function)

这个returns

[[1]]
[[1]]$my.table

1 
6 

[[1]]$my.binomial
[1] 1


[[2]]
[[2]]$my.table

10 20 
 3  3 

[[2]]$my.binomial
integer(0)


[[3]]
[[3]]$my.table

1 2 
3 3 

[[3]]$my.binomial
[1] 1

如果你想保留列表名称,你可以使用

sapply(names(my.list), table_function, simplify = FALSE, USE.NAMES = TRUE)

而不是 lapply

使用Map并将列表数据及其名称传递给函数。更改您的函数以接受两个参数。

table.function <- function(data, name) {
  
  # The three object names are:
  # 'matrix.apple1', 'matrix.orange1' and 'matrix.apple2'
  print(name)
  
  # perform a trivial example operation on a data.frame
  my.table <- table(as.matrix(data))
  
  # Test whether an object name contains the string 'apple'
  contains.apple <- grep('apple', name, fixed = TRUE)
  
  # Use the result of the above test to perform a trivial example operation.
  # With my code 'my.binomial' is always given the value of 0 even though
  # 'apple' appears in the name of two of the data.frames.
  my.binomial <- as.integer(contains.apple == 1)
  
  return(list(my.table = my.table, my.binomial = my.binomial))
}

Map(table.function, my.list, names(my.list))

#[1] "matrix.apple1"
#[1] "matrix.orange1"
#[1] "matrix.apple2"
#$matrix.apple1
#$matrix.apple1$my.table

#1 
#6 

#$matrix.apple1$my.binomial
#[1] 1


#$matrix.orange1
#$matrix.orange1$my.table

#10 20 
# 3  3 

#$matrix.orange1$my.binomial
#integer(0)
#...
#...

imappurrr 中提供了相同的功能,您无需显式传递名称。

purrr::imap(my.list, table.function)