[R] Datatable manipulation

arun smartpink111 at yahoo.com
Wed Nov 20 05:14:58 CET 2013


Hi,
Try:
dat1 <- read.table(text="a b c
x 1 4 7
y 2 5 8
z 3 6 9",header=TRUE)
dat2 <- dat1
#either
library(reshape2)
res1 <- within(melt(dat1,id.var="ID"),cell<-as.character(interaction(variable,ID,sep="_")))[,c(4,3)]

#or
indx <- which(dat2>0,arr.ind=TRUE) 
res2 <- data.frame(cell=paste(colnames(dat2)[indx[,2]],rownames(dat2)[indx[,1]],sep="_"),value=dat2[indx],stringsAsFactors=FALSE)
 identical(res1,res2)
#[1] TRUE

A.K.



On Tuesday, November 19, 2013 11:07 PM, Nitisha jha <nitisha999 at gmail.com> wrote:

Hi
could you please let me know the solution?
Thanks




On Wed, Nov 20, 2013 at 4:13 AM, <smartpink111 at yahoo.com> wrote:

Hi,
>If this is a data.frame(), then I have some solution for this problem.  Also, you need to post it in R-help if that is the case.
>Thanks.
>A.K.
><quote author='nuts'>
>Sample input dataset where a,b,c are col names. x y z are row names.
>        a       b       c
>x       1       4       7
>y       2       5       8
>z       3       6       9
>
>I want output to be like this after manipulating the dataset.
>cell    value
>a_x     1
>a_y     2
>a_z     3
>b_x     4
>b_y     5
>b_z     6
>c_x     7
>c_y     8
>c_z     9
>where cell and value are the colnames.
>How do I achieve this? I am at a loss here.
>
>
></quote>
>Quoted from:
>http://r.789695.n4.nabble.com/Datatable-manipulation-tp4680747.html
>
>
>_____________________________________
>Sent from http://r.789695.n4.nabble.com
>
>



More information about the R-help mailing list