Hello, I have a data as follow: ID Visit xa1 xa2 yb1 yc23 yb33 I want to look at frequency of visit for ID and create a new column as response . For example my response would be 2 for x and 3 for y. I think I need to write a loop, but I don't know how. I really appreciate your help. Thanks a lot. Best,Farnoosh Sheikhi [[alternative HTML version deleted]]
Are the data in a data frame? Is it the first letter of the id that defines unique ID's or something else? Depending on your answers: ?substring ?table ?with may be useful, as in with(yourdata,table(substring(ID,1,1))) ... or not, if you meant something else. -- Bert On Tue, Oct 23, 2012 at 12:14 PM, farnoosh sheikhi <farnoosh_81 at yahoo.com> wrote:> Hello, > > I have a data as follow: > ID Visit > xa1 > xa2 > yb1 > yc23 > yb33 > > I want to look at frequency of visit for ID and create a new column as response . > For example my response would be 2 for x and 3 for y. > I think I need to write a loop, but I don't know how. > I really appreciate your help. > Thanks a lot. > Best,Farnoosh Sheikhi > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hi, Assuming that there are two columns in your data. dat1<-read.table(text=" ID??? Visit x a1 x a2 y b1 y c23 y b33 ",sep="",header=TRUE,stringsAsFactors=FALSE) cnt<-count(dat1$ID) colnames(cnt)<-c("ID","response") merge(dat1,cnt,by="ID",all=TRUE) #? ID Visit response #1? x??? a1??????? 2 #2? x??? a2??????? 2 #3? y??? b1??????? 3 #4? y?? c23??????? 3 #5? y?? b33??????? 3 But, if there is only column and you want to split it ID<-c("xa1","xa2","yb1","yc23","yb33") ID1<-gsub("(.*)a.*|b.*|c.*","\\1",ID) ?visit<-gsub(".*(a.*|b.*|c.*)","\\1",ID) ?dat2<-data.frame(ID1,visit) dat2[]<-lapply(dat2,as.character) ?cnt<-count(dat2$ID1) colnames(cnt)<-c("ID1","response") ?merge(dat2,cnt,by="ID1",all=TRUE) #? ID1 visit response #1?? x??? a1??????? 2 #2?? x??? a2??????? 2 #3?? y??? b1??????? 3 #4?? y?? c23??????? 3 #5?? y?? b33??????? 3 A.K. ----- Original Message ----- From: farnoosh sheikhi <farnoosh_81 at yahoo.com> To: "r-help at R-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, October 23, 2012 3:14 PM Subject: [R] frequency Hello, I have a data as follow: ID ? ?Visit xa1 xa2 yb1 yc23 yb33 ? I want to look at frequency of visit for ID and create a new column as response .? For example my response would be 2 for x and 3 for y. I think I need to write a loop, but I don't know how. I really appreciate your help. Thanks a lot. Best,Farnoosh Sheikhi ??? [[alternative HTML version deleted]] ______________________________________________ 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??? Visit x a1 x a2 x a2 x a1 x a1 y b1 y c23 y b33 ",sep="",header=TRUE,stringsAsFactors=FALSE) cn1<-count(dat1[!duplicated(dat1),]$ID) colnames(cn1)<-c("ID","response") merge(dat1,cn1,by="ID",all=TRUE) #ID Visit response #1? x??? a1??????? 2 #2? x??? a2??????? 2 #3? x??? a2??????? 2 #4? x??? a1??????? 2 #5? x??? a1??????? 2 #6? y??? b1??????? 3 #7? y?? c23??????? 3 #8? y?? b33??????? 3 A.K. ________________________________ From: farnoosh sheikhi <farnoosh_81 at yahoo.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, October 23, 2012 4:55 PM Subject: Re: [R] frequency Hi there, Thanks a lot for getting back to me. If the data has repeated visit as follow, does the code change? Therefore the response would be 2 not 3. How can I consider this problem in my code? ID??? Visit x ? ? ? a1 x ? ? ?a2 xa1 y ? ? ?b1 y ? ? c23 y ? ? b33 Thanks a lot. Best,Farnoosh Sheikhi ________________________________ From: arun <smartpink111 at yahoo.com> To: farnoosh sheikhi <farnoosh_81 at yahoo.com> Cc: R help <r-help at r-project.org> Sent: Tuesday, October 23, 2012 1:33 PM Subject: Re: [R] frequency Hi, Assuming that there are two columns in your data. dat1<-read.table(text=" ID??? Visit x a1 x a2 y b1 y c23 y b33 ",sep="",header=TRUE,stringsAsFactors=FALSE) cnt<-count(dat1$ID) colnames(cnt)<-c("ID","response") merge(dat1,cnt,by="ID",all=TRUE) #? ID Visit response #1? x??? a1??????? 2 #2? x??? a2??????? 2 #3? y??? b1??????? 3 #4? y?? c23??????? 3 #5? y?? b33??????? 3 But, if there is only column and you want to split it ID<-c("xa1","xa2","yb1","yc23","yb33") ID1<-gsub("(.*)a.*|b.*|c.*","\\1",ID) ?visit<-gsub(".*(a.*|b.*|c.*)","\\1",ID) ?dat2<-data.frame(ID1,visit) dat2[]<-lapply(dat2,as.character) ?cnt<-count(dat2$ID1) colnames(cnt)<-c("ID1","response") ?merge(dat2,cnt,by="ID1",all=TRUE) #? ID1 visit response #1?? x??? a1??????? 2 #2?? x??? a2??????? 2 #3?? y??? b1??????? 3 #4?? y?? c23??????? 3 #5?? y?? b33??????? 3 A.K. ----- Original Message ----- From: farnoosh sheikhi <farnoosh_81 at yahoo.com> To: "r-help at R-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, October 23, 2012 3:14 PM Subject: [R] frequency Hello, I have a data as follow: ID ? ?Visit xa1 xa2 yb1 yc23 yb33 I want to look at frequency of visit for ID and create a new column as response .? For example my response would be 2 for x and 3 for y. I think I need to write a loop, but I don't know how. I really appreciate your help. Thanks a lot. Best,Farnoosh Sheikhi ??? [[alternative HTML version deleted]] ______________________________________________ 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.??