I have this sample matrix:
X1 X2 X3 X4
1 F F F F
2 C C C C
3 D D D D
4 A# A# A# A
And I'm trying to use a for loop to get the number of unique pitches in each column. Here's how I'm trying to do it:
y <- read.csv(file)
frame <- data.frame(y)
for(i in 1:4){
specframe <- frame[, i]
x <- unique(specframe)
}
length(x)
But the result of length is just 4. The output I'm looking for is a vector of 4 elements in which each element details the number of unique elements in their respective columns. It looks like the for loop is rewriting x every time it loops, so how would I make a vector which contains an element for each time it loops?
You could use
n_distinct
(wrapper forlength(unique(
) fromdplyr
This should be sufficient:
or: