Home > Net >  How do I use for loop to find unique values in all columns in a Dataframe
How do I use for loop to find unique values in all columns in a Dataframe

Time:04-21

enter image description here

I want to find out the unique values in every column in the dataframe using a for loop. Using names(df) stores the column names to a character datatype, which doesn't work in this case.

CodePudding user response:

This may be what you're looking for:

set.seed(123)
df <- data.frame(a = sample(1:100, 20),
                 b = sample(LETTERS, 20),
                 c = round(runif(20),2))

for(i in colnames(df)){
  cat("Unique values in", i, ":", unique(df[,i]), "\n")
}

Output:

#Unique values in a : 31 79 51 14 67 42 50 43 97 25 90 69 57 9 72 26 7 95 87 36 
#Unique values in b : N Q K G U L O J M W I P S Y X E R V C F 
#Unique values in c : 0.75 0.9 0.37 0.67 0.09 0.38 0.27 0.81 0.45 0.79 0.44 0.63 0.71 0 0.48 0.22 
  • Related