Sum in a Cross Table

I am new in R. Apologies if the questions is too silly.

I have a sample input data table as follows.

Column-1 Column-2 Column-3 
 FF       FF       2
 NN       NN       5
 FF       FF       2
 NN       NN       1
 FN       FF       3

Output should be

       FF   FN  NF  NN
    FF  4   0   0   0
    FN  3   0   0   0
    NF  0   0   0   0
    NN  0   0   0   6

I have used table() function, but it only gives "count", and not "sum". Appreciate any help!

Answers


With the little known xtabs function. Your data:

l <- c("FF", "FN", "NF", "NN")

data <- data.frame(
   Column1 = factor(c("FF", "NN", "FF", "NN", "FN"), levels = l),
   Column2 = factor(c("FF", "NN", "FF", "NN", "FF"), levels = l),
   Column3 = c(2, 5, 2, 1, 3))

xtabs(Column3 ~ ., data)
#        Column2
# Column1 FF FN NF NN
#      FF  4  0  0  0
#      FN  3  0  0  0
#      NF  0  0  0  0
#      NN  0  0  0  6

The output is a table but you can wrap it into as.matrix to get a matrix.


Need Your Help

Calling ExecutorService submit() inside an infinite loop

java multithreading executorservice

My question is regarding the shutdown() method from the ExecutorService class. If i am calling the submit() method in an infinite loop... do i ever need to call shutdown()? Essentially i have somet...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.