'How do I use for loop to find unique values in all columns in a Dataframe
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jpsmith |

