R: data conversion to table

I have a dataframe in R with three variables named, for example, df$V1 , df$V2 , df$V3 . df$V1 and df$V2 are both factors, and df$V3 are numerical.

 df <- data.frame( V1 = letters[1:4], V2 = rep(LETTERS[1:3], each = 4), V3 = 1:12 ) 

I am looking for a way to create a table containing values ​​in df$V3 , with df$V1 as rows and df$V2 as columns.

I tried the options on the table but received nothing. Maybe someone can help, Thanks in advance, Davy.

+8
r
source share
2 answers

This is an alternative to table :

 xtabs(V3 ~ V1 + V2, df) 
+17
source share

As mentioned in the ran2 file, you can use the reshape package. Here is an example:

 df <- data.frame(V1 = factor(sample(letters[1:5],100,replace=TRUE)), V2 = factor(toupper(sample(letters[1:5],100,replace=TRUE))), V3 = runif(100)) library(reshape) cast(df, V1 ~ V2, mean) 
+6
source share

All Articles