Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Counting Values | Factors
R Introduction: Part I

book
Counting Values

If you're looking to count the occurrences of each value within a vector, the summary() function is exactly what you need.

To tally the frequency of each level within a factor, use the summary() function with the factor as its argument. This will list the occurrences for each level that the factor can take. For instance:

# Factor variable
curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK',
'CHF', 'EUR', 'AUD', 'EUR'))
# Show summary
summary(curr_f)
12345
# Factor variable curr_f <- factor(c('USD', 'EUR', 'AUD', 'NOK', 'CHF', 'EUR', 'AUD', 'EUR')) # Show summary summary(curr_f)
copy

Remember, the sequence of the levels reported by summary() corresponds to the order defined when you used the factor() function. If no order is defined, it defaults to numerical or alphabetical order.

Compito

Swipe to start coding

For the grades data mentioned earlier, compute the frequency of each grade within the grades_f factor.

Soluzione

# Vector of grades and converting into factor
grades <- c("A","F","C","C","F","A","F","F","B",
"C","D","D","B","A","B","D","F","A",
"B","A","A","C","F","A","D","C")
grades_f <- factor(grades, labels = c('F', 'D', 'C', 'B', 'A'), ordered = T)
# Output the quantity of each possible value in grades_f
summary(grades_f)

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4
# Vector of grades and converting into factor
grades <- c("A","F","C","C","F","A","F","F","B",
"C","D","D","B","A","B","D","F","A",
"B","A","A","C","F","A","D","C")
grades_f <- factor(grades, labels = c('F', 'D', 'C', 'B', 'A'), ordered = T)
# Output the quantity of each possible value in grades_f
___(___)

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt