简体   繁体   中英

Return the unique character elements that are mixed with numeric ones in a data.frame

Suppose I have a data.frame that consists of mainly numeric values but also mixed with some character elements.

Is there a way to extract the unique character elements throughout the data.frame?

A toy example along with the desired output is shown below?

   DF <- data.frame(x = c(1:3, "*", "."), y = c("--", 4:6, "="), z = 1:5, w = rep("a", 5))

   desired_output <- c("*", ".", "--", "=")

You could extract all the values which has only punctuations in it from DF using grep :

unique(grep('^[[:punct:]]+$', as.character(unlist(DF)), value = TRUE))
#[1] "*"  "."  "--" "=" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM