Return data.frame 中与数字元素混合的唯一字符元素
Return the unique character elements that are mixed with numeric ones in a data.frame
假设我有一个 data.frame,主要由数值组成,但也混合了一些字符元素。
有没有办法在整个 data.frame 中提取 独特的 字符元素?
下面显示了一个玩具示例以及所需的输出?
DF <- data.frame(x = c(1:3, "*", "."), y = c("--", 4:6, "="), z = 1:5, w = rep("a", 5))
desired_output <- c("*", ".", "--", "=")
您可以使用 grep
从 DF
中提取所有只有标点符号的值:
unique(grep('^[[:punct:]]+$', as.character(unlist(DF)), value = TRUE))
#[1] "*" "." "--" "="
假设我有一个 data.frame,主要由数值组成,但也混合了一些字符元素。
有没有办法在整个 data.frame 中提取 独特的 字符元素?
下面显示了一个玩具示例以及所需的输出?
DF <- data.frame(x = c(1:3, "*", "."), y = c("--", 4:6, "="), z = 1:5, w = rep("a", 5))
desired_output <- c("*", ".", "--", "=")
您可以使用 grep
从 DF
中提取所有只有标点符号的值:
unique(grep('^[[:punct:]]+$', as.character(unlist(DF)), value = TRUE))
#[1] "*" "." "--" "="