1

is there a possibility to show data that is two-factorial in a table using R?

Please consider my example with replicates.

So one has two or more values in every cell.

I tried using ftable() and table() but am not getting anywhere :(

Thank you so much for helping

Example input

A    B    Value
1    1    1.2
1    1    1.4
1    2    2.1
1    2    2.0
2    1    1.1
2    1    1.2
2    2    3.1
2    2    3.1

Desired output, something like enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
feinmann
  • 1,060
  • 1
  • 14
  • 20
  • My primary intention is to illustrate the data, because I want to show it in a R Markdown Presentation. So I am looking for the R representation as "matrix" with two rows and two columns, where each cell holds two values, as in my example. – feinmann Dec 05 '14 at 12:07
  • possible duplicate of [Unique rows with multiple comma separated entries in R](http://stackoverflow.com/questions/8083984/unique-rows-with-multiple-comma-separated-entries-in-r) – Henrik Dec 05 '14 at 12:19
  • Thanks for your patience so far. You see, my problem (when described the right way) is pretty basic. – feinmann Dec 05 '14 at 12:23
  • @AnandaMahto Thank you very much. Is there a chance to name the columns and rows? There is just the simple 'A' in the upper left corner in my output und it is not clear, which values are B and A. But your solution is pretty nice :) And do you know the newline-character? collapse= '\n' does not work :( – feinmann Dec 05 '14 at 14:14

2 Answers2

3

Perhaps something like the following would be helpful:

library(data.table)
library(reshape2)
dcast.data.table(
  as.data.table(df)[, `:=`(A = paste0("A", A),   ## Prefix A values with "A"
                           B = paste0("B", B))], ## Prefix B values with "B"
  A ~ B, value.var = "Value",                    ## Our casting formula
  fun.aggregate = function(x)                    ## Our aggregation formula
    paste(sprintf("%2.2f", x), collapse = "/"))  ## sprintf -> uniform format
#     A        B1        B2
# 1: A1 1.20/1.40 2.10/2.00
# 2: A2 1.10/1.20 3.10/3.10

I've added comments in the code to explain what's going on at each step.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • First you need df<-read.table("mydata.txt"). Is it possible to paste with newlines? Very nice solution btw – feinmann Dec 05 '14 at 14:30
  • 1
    @feinmann, I don't think that you would be able to get new lines in the console this way. "\n" would need another escape, if you wanted to try it. – A5C1D2H2I1M1N2O1R2T1 Dec 05 '14 at 14:41
0

The tapply function is the standard base-R mechanism for getting afunction to be applied to a cross-classified value:

tapply(dat$Value, list(A=dat$A,B=dat$B) , function(x) paste(x,collapse="/"))
#----------
   B
A   1         2        
  1 "1.2/1.4" "2.1/2"  
  2 "1.1/1.2" "3.1/3.1"

If you wanted annotation:

tapply(dat$Value, list(A=dat$A,B=dat$B) , function(x) paste("V1=",x[1],"V2=",x[2]))
#--------------
   B
A   1                 2                
  1 "V1= 1.2 V2= 1.4" "V1= 2.1 V2= 2"  
  2 "V1= 1.1 V2= 1.2" "V1= 3.1 V2= 3.1"
IRTFM
  • 258,963
  • 21
  • 364
  • 487