An embedded and charset-unspecified text was scrubbed... Name: no disponible URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120904/041f1620/attachment.pl>
Create a function to number each repeated visit or measurements
6 messages · Jon Toledo, PIKAL Petr, arun +2 more
Hi You did not provided any suitable data. x<-sort(sample(letters[1:5], 50, rep=T)) unlist(lapply(split(x,x), function(x) (1:length(x))-1)) gives you a vector of indices from 0 to n for sorted vector x. Regards Petr
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Jon Toledo
Sent: Tuesday, September 04, 2012 7:59 AM
To: r-help at r-project.org
Subject: [R] Create a function to number each repeated visit or
measurements
Hi dear R list members,
I am trying to create a numerical variable that tracks the
visits/entries that a subject has had. For example having a database of
repeated serial measurements that are ordered by subject and time. So I
would get a variable that says 0 for baseline visit/measurement, 1 for
the second etc.
In my case the unique identifier for each subject is AreaID Will the
following code I can number the visits as long as I create as many
lines as visits.
But there is probably a way of coding a function or maybe there is
already a function.
DUP<-rep(0,length(AreaID))DUP[2:length(DUP)]<-
sapply(2:(length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-
1],1,DUP[x]))DUP[3:length(DUP)]<-sapply(3:(
length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-2],2,DUP[x]))
My Guess was:
AddLag<-function(x) { DUP<-c(rep(0,length(x))) for (i in
1:(max(as.numeric(GraphArtDB$Measure))+2)){
DUP[(i+1):length(x)]<-sapply(((i+1):length(x)),function(x)
ifelse(x[i]==x[x-i],i,DUP[x])) } return(DUP) }
But it didn?t work.
Any suggestions?
Thanks in advance
J ToledoUPennUSA
[[alternative HTML version deleted]]
Hi, May be you can also use ?aggregate(): x1<-aggregate(x,list(x),function(x) seq_along(x)-1) # Group.1??????????????????????????????????????????????????????????? x #1?????? a????????????????????????????????????????? 0, 1, 2, 3, 4, 5, 6 #2?????? b???????????????????????????????????????????? 0, 1, 2, 3, 4, 5 #3?????? c??????????????????????????????????? 0, 1, 2, 3, 4, 5, 6, 7, 8 #4?????? d???????????????????????????????? 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 #5?????? e 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 A.K. ----- Original Message ----- From: PIKAL Petr <petr.pikal at precheza.cz> To: Jon Toledo <tintin_jb at hotmail.com>; "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, September 4, 2012 7:07 AM Subject: Re: [R] Create a function to number each repeated visit or measurements Hi You did not provided any suitable data. x<-sort(sample(letters[1:5], 50, rep=T)) unlist(lapply(split(x,x), function(x) (1:length(x))-1)) gives you a vector of indices from 0 to n for sorted vector x. Regards Petr
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Jon Toledo
Sent: Tuesday, September 04, 2012 7:59 AM
To: r-help at r-project.org
Subject: [R] Create a function to number each repeated visit or
measurements
Hi dear R list members,
I am trying to create a numerical variable that tracks the
visits/entries that a subject has had. For example having a database of
repeated serial measurements that are ordered by subject and time. So I
would get a variable that says 0 for baseline visit/measurement, 1 for
the second etc.
In my case the unique identifier for each subject is AreaID Will the
following code I can number the visits as long as I create as many
lines as visits.
But there is probably a way of coding a function or maybe there is
already a function.
DUP<-rep(0,length(AreaID))DUP[2:length(DUP)]<-
sapply(2:(length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-
1],1,DUP[x]))DUP[3:length(DUP)]<-sapply(3:(
length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-2],2,DUP[x]))
My Guess was:
AddLag<-function(x) {??? ??? DUP<-c(rep(0,length(x)))??? for (i in
1:(max(as.numeric(GraphArtDB$Measure))+2)){
??? DUP[(i+1):length(x)]<-sapply(((i+1):length(x)),function(x)
ifelse(x[i]==x[x-i],i,DUP[x]))??? }??? ??? return(DUP)??? ??? }
But it didn?t work.
Any suggestions?
Thanks in advance
J ToledoUPennUSA
??? [[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.
An embedded and charset-unspecified text was scrubbed... Name: no disponible URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120904/baa30f3a/attachment.pl>
For unsorted x, or for a factor x with unused levels, ave(integer(length(x)), x, FUN=seq_along) - 1 works. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of PIKAL Petr Sent: Tuesday, September 04, 2012 4:08 AM To: Jon Toledo; r-help at r-project.org Subject: Re: [R] Create a function to number each repeated visit or measurements Hi You did not provided any suitable data. x<-sort(sample(letters[1:5], 50, rep=T)) unlist(lapply(split(x,x), function(x) (1:length(x))-1)) gives you a vector of indices from 0 to n for sorted vector x. Regards Petr
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Jon Toledo
Sent: Tuesday, September 04, 2012 7:59 AM
To: r-help at r-project.org
Subject: [R] Create a function to number each repeated visit or
measurements
Hi dear R list members,
I am trying to create a numerical variable that tracks the
visits/entries that a subject has had. For example having a database of
repeated serial measurements that are ordered by subject and time. So I
would get a variable that says 0 for baseline visit/measurement, 1 for
the second etc.
In my case the unique identifier for each subject is AreaID Will the
following code I can number the visits as long as I create as many
lines as visits.
But there is probably a way of coding a function or maybe there is
already a function.
DUP<-rep(0,length(AreaID))DUP[2:length(DUP)]<-
sapply(2:(length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-
1],1,DUP[x]))DUP[3:length(DUP)]<-sapply(3:(
length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-2],2,DUP[x]))
My Guess was:
AddLag<-function(x) { DUP<-c(rep(0,length(x))) for (i in
1:(max(as.numeric(GraphArtDB$Measure))+2)){
DUP[(i+1):length(x)]<-sapply(((i+1):length(x)),function(x)
ifelse(x[i]==x[x-i],i,DUP[x])) } return(DUP) }
But it didn?t work.
Any suggestions?
Thanks in advance
J ToledoUPennUSA
[[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.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Jon Toledo
Sent: Monday, September 03, 2012 10:59 PM
To: r-help at r-project.org
Subject: [R] Create a function to number each repeated visit or
measurements
Hi dear R list members,
I am trying to create a numerical variable that tracks the
visits/entries that a subject has had. For example having a database of
repeated serial measurements that are ordered by subject and time. So I
would get a variable that says 0 for baseline visit/measurement, 1 for
the second etc.
In my case the unique identifier for each subject is AreaID
Will the following code I can number the visits as long as I create as
many lines as visits.
But there is probably a way of coding a function or maybe there is
already a function.
DUP<-rep(0,length(AreaID))DUP[2:length(DUP)]<-
sapply(2:(length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-
1],1,DUP[x]))DUP[3:length(DUP)]<-sapply(3:(
length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-2],2,DUP[x]))
My Guess was:
AddLag<-function(x) { DUP<-c(rep(0,length(x))) for (i in
1:(max(as.numeric(GraphArtDB$Measure))+2)){
DUP[(i+1):length(x)]<-sapply(((i+1):length(x)),function(x)
ifelse(x[i]==x[x-i],i,DUP[x])) } return(DUP) }
But it didn?t work.
Any suggestions?
Thanks in advance
Your email was scrambled and therefore difficult to decipher. However, using the plyr package, you could do something like library(plyr) ddply(your_data_frame,"AreaID",transform,visit=0:(length(AreaID)-1)) Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204