This seems to be basic, but I wont get it. I am trying to compute the frequency table in R for the data as below
1 2
2 1
3 1
I want to
The data:
df <- read.table(text = "1 2
2 1
3 1")
Calculate frequencies using table
:
(If your object is a matrix, you could convert it to a data frame using as.data.frame
before using table
.)
tab <- table(df)
V2
V1 1 2
1 0 1
2 1 0
3 1 0
Write data with the function write.csv
:
write.csv(tab, "tab.csv")
The resulting file:
"","1","2"
"1",0,1
"2",1,0
"3",1,0