-2

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.

  • Please share your data using `dput(df)`? – Quinten Apr 21 '22 at 11:10
  • Please do not post screenshots of code - we can't transfer that into R. Instead add code and data as text. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info. – Andrea M Apr 21 '22 at 11:11
  • 1
    `names(df)` can be used with the square bracket operator for subsetting a data frame. Try `for(col in names(df)) { print(glue("Unique values in {col} variable:", unique(df[col])) }` – Allan Cameron Apr 21 '22 at 11:14
  • Please explain what you mean by *"doesn't work in this case"*. – Rui Barradas Apr 21 '22 at 11:15

1 Answers1

2

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 
jpsmith
  • 11,023
  • 5
  • 15
  • 36