I have a very simple dataset and I tried to perform table() on the first column of the table, but R returns a captioned error message. I searched online but don't quite understand why this would even happen, as R accepts my table as a table... could anyone advise?
My expect output:
> table(tab$V1)
CA 1
CO 1
OH 2
However it returns:
> tabraw
V1 V2
1 OH Cleveland
2 OH Columbus
3 CO Denver
4 CA SanFran
> tab <- table(tabraw)
> tab
V2
V1 Cleveland Columbus Denver SanFran
CA 0 0 0 1
CO 0 0 1 0
OH 1 1 0 0
> table(tab$V1)
Error in tab$V1 : $ operator is invalid for atomic vectors
Such an error is produced by the R compiler when we try to get an element of an atomic vector by using the $ operator. An atomic vector is simply a 1-dimensional object containing data created with the help of c() and vector() functions.
Atomic vectors are probably the most fundamental data structure in the R programming language. An atomic vector is different from a one-dimensional array: an array has a dim attribute of length one while a vector has no such attribute. An atomic vector is also different from a list.
You are looking for
table(tabraw$V1)
#
# CA CO OH
# 1 1 2
The object tab
is an object of class table and doesn't support the $
function.
You can also obtain the desired information from the tab
object with
rowSums(tab)
# CA CO OH
# 1 1 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With