HI,
You can do this in many ways:
dat1<-read.table(text="
med1,med2,med3????
?1,0,1??????
0,1,1???
2,0,0
",sep=",",header=TRUE)??
#1st method
library(reshape)
dat2<-melt(dat1)
dat3<-aggregate(dat2$value,by=list(dat2$variable),sum)
?colnames(dat3)<-c("name","sum(n11)")
?dat3
#? name sum(n11)
#1 med1??????? 3
#2 med2??????? 1
#3 med3??????? 2
#2nd method
res<-data.frame(colSums(dat1))
?names(res)<-"sum(n11)"
?res
#???? sum(n11)
#med1??????? 3
#med2??????? 1
#med3??????? 2
#3rd method
?do.call(rbind,lapply(dat1,sum))
#???? [,1]
#med1??? 3
#med2??? 1
#med3??? 2
A.K.
________________________________
From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Monday, November 12, 2012 7:24 PM
Subject: for loop
Hi there,
I want to calculate the odds ratio for a data like below.
?I want to compute the sum of each column as a new column and variable names as
a new column.
I have about 1000 variables and I think I need to write a loop.
med1 ? ?med2 ? ?med3 ? ??
?1 ? ? ? ? ? ? ?01 ? ? ??
011
200 ? ? ??
The final data will look like:
name ? ? ? ?sum(n11)
med1 ? ? ? ? 3
med2 ? ? ? ? ?1
med3 ? ? ? ? ?2
Thanks a lot :-).
Best,Farnoosh Sheikhi
thanks dear. method 2 worked very fast since my data is very big.
Thanks a lot. :-)
Best,Farnoosh Sheikhi
________________________________
Cc: R help <r-help@r-project.org>
Sent: Monday, November 12, 2012 6:15 PM
Subject: Re: for loop
HI,
You can do this in many ways:
dat1<-read.table(text="
med1,med2,med3
1,0,1
0,1,1
2,0,0
",sep=",",header=TRUE)
#1st method
library(reshape)
dat2<-melt(dat1)
dat3<-aggregate(dat2$value,by=list(dat2$variable),sum)
colnames(dat3)<-c("name","sum(n11)")
dat3
# name sum(n11)
#1 med1 3
#2 med2 1
#3 med3 2
#2nd method
res<-data.frame(colSums(dat1))
names(res)<-"sum(n11)"
res
# sum(n11)
#med1 3
#med2 1
#med3 2
#3rd method
do.call(rbind,lapply(dat1,sum))
# [,1]
#med1 3
#med2 1
#med3 2
A.K.
________________________________
Sent: Monday, November 12, 2012 7:24 PM
Subject: for loop
Hi there,
I want to calculate the odds ratio for a data like below.
I want to compute the sum of each column as a new column and variable names as
a new column.
I have about 1000 variables and I think I need to write a loop.
med1 med2 med3
1 01
011
200
The final data will look like:
name sum(n11)
med1 3
med2 1
med3 2
Thanks a lot :-).
Best,Farnoosh Sheikhi
[[alternative HTML version deleted]]
Hello user, I have large data containing subject id, time and response where subjects are measured repeatedly. However some time are duplicates. I only want data with unique time points per id. I mean if time is repeated, then take only one. Here is a sample data. id time res 1 2 0.64 1 3 0.78 1 3 6.5 1 3 4.5 1 4 4 1 5 3.4 2 10 5.7 2 11 5.8 2 11 9.3 2 11 3.4 2 12 3.4 2 13 6.7 3 3 5.6 3 3 3.4 3 4 2.3 3 5 5.6 3 12 9.8 3 10 7 3 24 6 3 16 4 for 1st subject I want this, id time res 1 2 0.64 1 3 0.78 1 4 4 1 5 3.4 Any suggestions are much appreciated! Thanks, Bikek
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of bibek sharma > Sent: Friday, November 30, 2012 5:00 PM > To: R-help at r-project.org > Subject: [R] For loop > > Hello user, > I have large data containing subject id, time and response where > subjects are measured repeatedly. However some time are duplicates. I > only want data with unique time points per id. I mean if time is > repeated, then take only one.Which one? The first one? aggregate(test$res, list(time=test$time, id=test$id), function(x) x[1]) Regards Petr> Here is a sample data. > > id time res > 1 2 0.64 > 1 3 0.78 > 1 3 6.5 > 1 3 4.5 > 1 4 4 > 1 5 3.4 > 2 10 5.7 > 2 11 5.8 > 2 11 9.3 > 2 11 3.4 > 2 12 3.4 > 2 13 6.7 > 3 3 5.6 > 3 3 3.4 > 3 4 2.3 > 3 5 5.6 > 3 12 9.8 > 3 10 7 > 3 24 6 > 3 16 4 > > for 1st subject I want this, > > id time res > 1 2 0.64 > 1 3 0.78 > 1 4 4 > 1 5 3.4 > Any suggestions are much appreciated! > Thanks, > Bikek > > ______________________________________________ > 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.
Hi, Try this: dat1<-read.table(text=" id??? time???? res 1??? 2??? 0.64 1??? 3??? 0.78 1??? 3??? 6.5 1??? 3??? 4.5 1??? 4??? 4 1??? 5??? 3.4 2??? 10??? 5.7 2??? 11??? 5.8 2??? 11??? 9.3 2??? 11??? 3.4 2??? 12??? 3.4 2??? 13??? 6.7 3??? 3??? 5.6 3??? 3??? 3.4 3??? 4??? 2.3 3??? 5??? 5.6 3??? 12??? 9.8 3??? 10??? 7 3??? 24??? 6 3??? 16??? 4 ",sep="",header=TRUE,stringsAsFactors=TRUE) res<-dat1[!duplicated(dat1[,1:2]),] ?res #?? id time? res #1?? 1??? 2 0.64 #2?? 1??? 3 0.78 #5?? 1??? 4 4.00 #6?? 1??? 5 3.40 #7?? 2?? 10 5.70 #8?? 2?? 11 5.80 #11? 2?? 12 3.40 #12? 2?? 13 6.70 #13? 3??? 3 5.60 #15? 3??? 4 2.30 #16? 3??? 5 5.60 #17? 3?? 12 9.80 #18? 3?? 10 7.00 #19? 3?? 24 6.00 #20? 3?? 16 4.00 row.names(res)<-1:nrow(res) A.K. ----- Original Message ----- From: bibek sharma <mbhpathak at gmail.com> To: R-help at r-project.org Cc: Sent: Friday, November 30, 2012 10:59 AM Subject: [R] For loop Hello user, I have large data containing? subject id, time and response where subjects are measured repeatedly. However some time are duplicates. I only want data with unique time points per id. I mean if time is repeated, then take only one. Here is a sample data. id??? time ??? res 1??? 2??? 0.64 1??? 3??? 0.78 1??? 3??? 6.5 1??? 3??? 4.5 1??? 4??? 4 1??? 5??? 3.4 2??? 10??? 5.7 2??? 11??? 5.8 2??? 11??? 9.3 2??? 11??? 3.4 2??? 12??? 3.4 2??? 13??? 6.7 3??? 3??? 5.6 3??? 3??? 3.4 3??? 4??? 2.3 3??? 5??? 5.6 3??? 12??? 9.8 3??? 10??? 7 3??? 24??? 6 3??? 16??? 4 for 1st subject I want this, id??? time ??? res 1??? 2??? 0.64 1??? 3??? 0.78 1??? 4??? 4 1??? 5??? 3.4 Any suggestions are much appreciated! Thanks, Bikek ______________________________________________ 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.