[R] R riddle
arun
smartpink111 at yahoo.com
Mon Jan 13 18:18:12 CET 2014
Hi,
Try:
dat1 <- read.table(text="I V3 X210505 X210505.1 X417802 X417802.1 X420385 X420385.1 X420480 X420480.1
A A3205 B A B A B A B A
M A3662 A B B B A B B B
M AR0012 B B B B A B B B
M AR0232 B B B B B B B A
M AR0341 A A B A B A A B
M AR0372 B B A A A B A B
M A0051 B B B B B B B B
M AR0549 A B B A B A B B",sep="",header=TRUE,stringsAsFactors=FALSE)
library(reshape2)
res <- as.data.frame(acast(transform(melt(dat1[,-1],id.var="V3"),variable=gsub("\\..*","",variable)),variable~V3,value.var="value",function(x) paste(x,collapse=" ")),stringsAsFactors=FALSE)
res1 <- res[,match(dat1$V3,colnames(res))]
res1
# A3205 A3662 AR0012 AR0232 AR0341 AR0372 A0051 AR0549
#X210505 B A A B B B B B A A B B B B A B
#X417802 B A B B B B B B B A A A B B B A
#X420385 B A A B A B B B B A A B B B B A
#X420480 B A B B B B B A A B A B B B B B
A.K.
For this I thought that will be way easier, but it is impossible for me (again) (and I just started to like R :))
With this example I want to get
I V3 X210505 X210505.1 X417802 X417802.1 X420385 X420385.1 X420480 X420480.1
A A3205 B A B A B A B A
M A3662 A B B B A B B B
M AR0012 B B B B A B B B
M AR0232 B B B B B B B A
M AR0341 A A B A B A A B
M AR0372 B B A A A B A B
M A0051 B B B B B B B B
M AR0549 A B B A B A B B
this:
A3205 A3662 AR0012 AR0232 AR0341 AR0372 A0051 AR0549
X210505 B A A B B B B B A A B B B B A B
X417802 B A B B B B B B B A A A B B B A
X420385 B A A B A B B B B A A B B B B A
X420480 B A B B B B B A A B A B B B B B
again, no idea...
On Friday, January 10, 2014 9:50 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Use ?reshape()
dat1<- read.table(text="V1 V2 V3 V4 V5
1 210505 ARS B A
1 210505 BFGL A B
1 210505 NGS B B
1 210506 ARS B B
1 210506 BFGL A A
1 210506 NGS B B
1 210507 ARS B B
1 210507 BFGL A B
1 210507 NGS A B",sep="",header=TRUE,stringsAsFactors=FALSE)
res <- reshape(dat1[,-1],timevar="V2",idvar="V3",direction="wide")
colnames(res)[-1] <-gsub(".*\\.","",colnames(res)[-1])
res
# V3 210505 210505 210506 210506 210507 210507
#1 ARS B A B B B B
#2 BFGL A B A A A B
#3 NGS B B B B A B
A.K.
Dear R users I have a question.
I have example file:
V1 V2 V3 V4 V5
1 210505 ARS B A
1 210505 BFGL A B
1 210505 NGS B B
1 210506 ARS B B
1 210506 BFGL A A
1 210506 NGS B B
1 210507 ARS B B
1 210507 BFGL A B
1 210507 NGS A B
from which I want to get file which looks like this:
V2 210505 210505 210506 210506 210507 210507
ARS B A B B B B
BFGL A B A A A B
NGS B B B B A B
I think this can be done with a loop but unfortunatelly I'm not experienced with loops.
Thank you very much for help!
More information about the R-help
mailing list