简体   繁体   中英

how to cross tabulation output as dataset

I used the code below for cross tabulation:

data=table(subset(datGSS, select = c("sex", "happy")))

In this result format is saved into values format but I would need output saved into dataset format.

Can any one help me in this regard?

You can pass the result of table to data.frame (or as.data.frame ). I guess, data.frame(data) should do the job for you but here is an example on a simple data.frame (your example was not reproducible ) :

df <- data.frame(col1=factor(letters[1:4]), 
                 col2=factor(LETTERS[1:2]))
> table(df)
col2
col1 A B
a 1 0
b 0 1
c 1 0
d 0 1

To convert it to a data.frame (the "dataset" class does not exist in R):

> as.data.frame(table(df))
  col1 col2 Freq
1    a    A    1
2    b    A    0
3    c    A    1
4    d    A    0
5    a    B    0
6    b    B    1
7    c    B    0
8    d    B    1

Is this what you want?

Also, note that RStudio is "just" an IDE for R. So that's more an R question than an RStudio one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM