count equal values in a vector
# Sample vector
my_vector <- c(1, 2, 3, 2, 4, 2, 5, 6, 5, 2)
# Use table function to count occurrences of each unique value
value_counts <- table(my_vector)
# Display the counts
print(value_counts)
Explanation:
my_vector <- c(1, 2, 3, 2, 4, 2, 5, 6, 5, 2)
: Creates a sample vector namedmy_vector
with some numeric values.value_counts <- table(my_vector)
: Uses thetable
function to count the occurrences of each unique value in the vector. The result is stored in the variablevalue_counts
.print(value_counts)
: Prints the counts of each unique value in the vector to the console.