Dear all, I have a dataset that looks like this: x <- read.table(textConnection("col1 col2 3 1 2 2 4 7 8 6 5 10"), header=TRUE) I want to rewrite it as below: var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 1 0 1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 Can anybody please help how to get this done? Your help would be greatly appreciated. Lisa -- View this message in context: http://n4.nabble.com/Data-transformation-tp1289899p1289899.html Sent from the R help mailing list archive at Nabble.com.
Well, I have no idea how to get from one to the other. There's col1 and col2 but no var1 var2 var3, etc. I thought perhaps col1 was the row index and col2 was the column index, but that doesn't match up either, and not all the cell values are 1. So you will need to explain more clearly what you intend. Meanwhile, you might try reshape, or perhaps crosstab from the ecodist package. Sarah On Mon, Jan 25, 2010 at 5:39 PM, Lisa <lisajca at gmail.com> wrote:> > Dear all, > > I ?have a dataset that looks like this: > > x <- read.table(textConnection("col1 col2 > 3 1 > 2 2 > 4 7 > 8 6 > 5 10"), header=TRUE) > > I want to rewrite it as below: > > var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 > ? ?1 ? ? 0 ? ? 1 ? ? ?0 ? ? 0 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 2 ? ? 0 ? ? ?0 ? ? 0 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?1 ? ? 0 ? ? 0 ? ? 1 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?0 ? ? 0 ? ? 1 ? ? 0 ? ? 1 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?0 ? ? 1 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?1 > > Can anybody please help how to get this done? Your help would be greatly > appreciated. > > Lisa >-- Sarah Goslee http://www.functionaldiversity.org
Hi, On Mon, Jan 25, 2010 at 5:39 PM, Lisa <lisajca at gmail.com> wrote:> > Dear all, > > I ?have a dataset that looks like this: > > x <- read.table(textConnection("col1 col2 > 3 1 > 2 2 > 4 7 > 8 6 > 5 10"), header=TRUE) > > I want to rewrite it as below: > > var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 > ? ?1 ? ? 0 ? ? 1 ? ? ?0 ? ? 0 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 2 ? ? 0 ? ? ?0 ? ? 0 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?1 ? ? 0 ? ? 0 ? ? 1 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?0 ? ? 0 ? ? 1 ? ? 0 ? ? 1 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?0 ? ? 1 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?1 > > Can anybody please help how to get this done? Your help would be greatly > appreciated.I was trying to do it w/o for loops, but I can't figure out a way to do so: R> bounds <- range(x) R> m <- matrix(0, nrow=nrow(x), ncol=bounds[2]) R> colnames(m) <- paste('var', seq(bounds[2]), sep="") ## Ugly nested for-loop one-liner below R> for (i in 1:nrow(x)) for (j in 1:ncol(x)) m[i,x[i,j]] <- m[i,x[i,j]] + 1 R> m var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 [1,] 1 0 1 0 0 0 0 0 0 0 [2,] 0 2 0 0 0 0 0 0 0 0 [3,] 0 0 0 1 0 0 1 0 0 0 [4,] 0 0 0 0 0 1 0 1 0 0 [5,] 0 0 0 0 1 0 0 0 0 1 -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Thank you so much. Lisa -- View this message in context: http://n4.nabble.com/Data-transformation-tp1289899p1289915.html Sent from the R help mailing list archive at Nabble.com.
r-help-bounces@r-project.org wrote on 01/25/2010 02:39:32 PM:> x <- read.table(textConnection("col1 col2 > 3 1 > 2 2 > 4 7 > 8 6 > 5 10"), header=TRUE) > > I want to rewrite it as below: > > var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 > 1 0 1 0 0 0 0 0 0 0 > 0 2 0 0 0 0 0 0 0 0 > 0 0 0 1 0 0 1 0 0 0 > 0 0 0 0 0 1 0 1 0 0 > 0 0 0 0 1 0 0 0 0 1 > > Can anybody please help how to get this done? Your help would be greatly > appreciated.Thanks, I've not seen textConnection() before. The table() function will get you close: table(c(rownames(x),rownames(x)), c(x$col1,x$col2)) cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]
Try this:> t(apply(x, 1, function(r) table(factor(r, levels = seq_len(max(x))))))1 2 3 4 5 6 7 8 9 10 [1,] 1 0 1 0 0 0 0 0 0 0 [2,] 0 2 0 0 0 0 0 0 0 0 [3,] 0 0 0 1 0 0 1 0 0 0 [4,] 0 0 0 0 0 1 0 1 0 0 [5,] 0 0 0 0 1 0 0 0 0 1 If you use aaply in the plyr package instead of apply then you can omit the transpose. On Mon, Jan 25, 2010 at 5:39 PM, Lisa <lisajca at gmail.com> wrote:> > Dear all, > > I ?have a dataset that looks like this: > > x <- read.table(textConnection("col1 col2 > 3 1 > 2 2 > 4 7 > 8 6 > 5 10"), header=TRUE) > > I want to rewrite it as below: > > var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 > ? ?1 ? ? 0 ? ? 1 ? ? ?0 ? ? 0 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 2 ? ? 0 ? ? ?0 ? ? 0 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?1 ? ? 0 ? ? 0 ? ? 1 ? ? 0 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?0 ? ? 0 ? ? 1 ? ? 0 ? ? 1 ? ? ?0 ? ? ?0 > ? ?0 ? ? 0 ? ? 0 ? ? ?0 ? ? 1 ? ? 0 ? ? 0 ? ? 0 ? ? ?0 ? ? ?1 > > Can anybody please help how to get this done? Your help would be greatly > appreciated. > > Lisa > > -- > View this message in context: http://n4.nabble.com/Data-transformation-tp1289899p1289899.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >