Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R saving the output of table() into a data frame

Tags:

r

I have the following data frame:

id<-c(1,2,3,4,1,1,2,3,4,4,2,2)
period<-c("first","calib","valid","valid","calib","first","valid","valid","calib","first","calib","valid")
df<-data.frame(id,period)

typing

table(df) 

results in

period
id  calib first valid
1     1     2     0
2     2     0     2
3     0     0     2
4     1     1     1

however if I save it as a data frame 'df'

 df<-data.frame(table(df))

the format of 'df' would be like

id period Freq
1   1  calib    2
2   2  calib    1
3   3  calib    1
4   4  calib    0
5   1  first    1
6   2  first    2
7   3  first    0
8   4  first    0
9   1  valid    0
10  2  valid    0
11  3  valid    2
12  4  valid    3

how can I avoid this and how can I save the first output as it is into a data frame?

more importantly is there any way to get the same result using 'dcast'?

like image 887
AliCivil Avatar asked Jul 21 '12 10:07

AliCivil


People also ask

How do I turn a table into a DataFrame in R?

data. frame() function converts a table to a data frame in a format that you need for regression analysis on count data. If you need to summarize the counts first, you use table() to create the desired table. Now you get a data frame with three variables.

How do I save a value to a DataFrame in R?

To save data as an RData object, use the save function. To save data as a RDS object, use the saveRDS function. In each case, the first argument should be the name of the R object you wish to save. You should then include a file argument that has the file name or file path you want to save the data set to.

Is a table a data frame in R?

Data Visualization using R ProgrammingA data frame is a table or a two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column. Following are the characteristics of a data frame. The column names should be non-empty.


2 Answers

Would this help?

> data.frame(unclass(table(df)))
  calib first valid
1     1     2     0
2     2     0     2
3     0     0     2
4     1     1     1
like image 91
johannes Avatar answered Sep 23 '22 14:09

johannes


To elaborate just a little bit. I've changed the ids in the example data.frame such that your ids are not 1:4, in order to prove that the ids are carried along into the table and are not a sequence of row counts.

id <- c(10,20,30,40,10,10,20,30,40,40,20,20)    
period <- c("first","calib","valid","valid","calib","first","valid","valid","calib","first","calib","valid")
df <- data.frame(id,period)

Create the new data.frame one of two ways. rengis answer is fine for 2-column data frames that have the id column first. It won't work so well if your data frame has more than 2 columns, or if the columns are in a different order.

Alternative would be to specify the columns and column order for your table:

df3 <- data.frame(unclass(table(df$id, df$period)))

the id column is included in the new data.frame as row.names(df3). To add it as a new column:

df3$id <- row.names(df3)
df3
   calib first valid id
10     1     2     0 10
20     2     0     2 20
30     0     0     2 30
40     1     1     1 40
like image 42
Brian D Avatar answered Sep 24 '22 14:09

Brian D