Re: [R] how to change the date into an interval of date?
Hi, Try: evt_c.1- read.table(text= patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 2 2010-5 2 2010-6 2 2010-7 ,sep=,header=TRUE,stringsAsFactors=FALSE) lst1-split(evt_c.1,evt_c.1$patient_id) res-do.call(rbind,lapply(lst1,function(x) {x1-as.numeric(gsub(.*\\-,,x[,2])); x$t-c(0,cumsum(diff(x1)));x})) row.names(res)-1:nrow(res) res # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 2 2010-5 0 #6 2 2010-6 1 #7 2 2010-7 2 #or library(plyr) res2-mutate(evt_c.1,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x) res2 # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 2 2010-5 0 #6 2 2010-6 1 #7 2 2010-7 2 identical(res,res2) #[1] TRUE A.K. From: GUANGUAN LUO guanguan...@gmail.com To: arun smartpink...@yahoo.com Sent: Wednesday, April 17, 2013 8:32 AM Subject: Re: how to change the date into an interval of date? thank you, and now i've got a table like this dput(head(evt_c.1,5)) structure(list(responsed_at = c(2010-05, 2010-07, 2010-08, 2010-10, 2010-11), patient_id = c(2L, 2L, 2L, 2L, 2L), number = c(1, 2, 3, 4, 5), response_id = c(77L, 1258L, 2743L, 4499L, 6224L), session_id = c(2L, 61L, 307L, 562L, 809L), login = c(3002, 3002, 3002, 3002, 3002), clinique_basdai.fatigue = c(4, 5, 5, 6, 4), which i want is to add a column t, for example now my table is like this: patient_id responsed_at 12010-5 12010-7 12010-8 12010-9 22010-5 22010-6 22010-7 after add the column t paient_id responsed_att 12010-5 0 12010-7 2 12010-8 3 12010-9 4 22010-5 0 22010-6 1 22010-7 2 Le 17 avril 2013 14:23, arun smartpink...@yahoo.com a écrit : Hi, format() is one way. library(zoo) as.yearmon(dat1$responsed_at) #[1] May 2010 Jul 2010 Aug 2010 Oct 2010 Nov 2010 Dec 2010 #[7] Jan 2011 Feb 2011 Mar 2011 Apr 2011 Jun 2011 Jul 2011 #[13] Aug 2011 Sep 2011 Oct 2011 Nov 2011 Dec 2011 Jan 2012 #[19] Mar 2012 May 2010 A.K. __ R-help@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.
Re: [R] how to change the date into an interval of date?
Hi, I hope this is what you are looking for: library(plyr) mutate(evt_c.1,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,gsub(-.*,,responsed_at),FUN=function(x) c(0,cumsum(diff(x) # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 1 2010-12 7 #6 1 2011-1 0 #7 1 2011-2 1 #8 2 2010-5 0 #9 2 2010-6 1 #10 2 2010-7 2 #11 3 2010-1 0 #12 3 2010-2 1 #13 3 2010-4 3 #14 3 2010-5 4 #15 4 2011-01 0 #16 4 2011-03 2 #17 5 2012-04 0 #18 5 2012-06 2 A.K. From: GUANGUAN LUO guanguan...@gmail.com To: arun smartpink...@yahoo.com Sent: Wednesday, April 17, 2013 9:21 AM Subject: Re: how to change the date into an interval of date? evt_c.1- read.table(text= patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 1 2010-12 1 2011-1 1 2011-2 2 2010-5 2 2010-6 2 2010-7 3 2010-1 3 2010-2 3 2010-4 3 2010-5 4 2011-01 4 2011-03 5 2012-04 5 2012-06 ,sep=,header=TRUE, stringsAsFactors=FALSE) mutate(evt_c.11,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x) patient_id responsed_at t 1 1 2010-5 0 2 1 2010-7 2 3 1 2010-8 3 4 1 2010-9 4 5 1 2010-12 7 6 1 2011-1 -4 7 1 2011-2 -3 8 2 2010-5 0 9 2 2010-6 1 10 2 2010-7 2 11 3 2010-1 0 12 3 2010-2 1 13 3 2010-4 3 14 3 2010-5 4 15 4 2011-01 0 16 4 2011-03 2 17 5 2012-04 0 18 5 2012-06 2 This is my problem. __ R-help@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.
Re: [R] how to change the date into an interval of date?
Hi, Try this: library(mondate) mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],-01,sep=,patient_id,FUN=function(x) c(0,cumsum(diff(x) # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 1 2010-12 7 #6 1 2011-1 8 #7 1 2011-2 9 #8 2 2010-5 0 #9 2 2010-6 1 #10 2 2010-7 2 #11 3 2010-1 0 #12 3 2010-2 1 #13 3 2010-4 3 #14 3 2010-5 4 #15 4 2011-01 0 #16 4 2011-03 2 #17 5 2012-04 0 #18 5 2012-06 2 If it change: evt_c.1$responsed_at[6:7]- c(2011-05,2011-07) mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],-01,sep=,patient_id,FUN=function(x) c(0,cumsum(diff(x) # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 1 2010-12 7 #6 1 2011-05 12 #7 1 2011-07 14 #8 2 2010-5 0 #9 2 2010-6 1 #10 2 2010-7 2 #11 3 2010-1 0 #12 3 2010-2 1 #13 3 2010-4 3 #14 3 2010-5 4 #15 4 2011-01 0 #16 4 2011-03 2 #17 5 2012-04 0 #18 5 2012-06 2 A.K. From: GUANGUAN LUO guanguan...@gmail.com To: arun smartpink...@yahoo.com Sent: Wednesday, April 17, 2013 9:25 AM Subject: Re: how to change the date into an interval of date? mutate(evt_c.11,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x) patient_id responsed_at t 1 1 2010-5 0 2 1 2010-7 2 3 1 2010-8 3 4 1 2010-9 4 5 1 2010-12 7 6 1 2011-1 8 7 1 2011-2 9 8 2 2010-5 0 9 2 2010-6 1 10 2 2010-7 2 11 3 2010-1 0 12 3 2010-2 1 13 3 2010-4 3 14 3 2010-5 4 15 4 2011-01 0 16 4 2011-03 2 17 5 2012-04 0 18 5 2012-06 2 this is the order i want. you are so kind-hearted. GG 2013/4/17 arun smartpink...@yahoo.com Alright, Sorry, I misunderstood. So, what do you want your result to be at 2011-1. Is it 0? From: GUANGUAN LUO guanguan...@gmail.com To: arun smartpink...@yahoo.com Sent: Wednesday, April 17, 2013 9:21 AM Subject: Re: how to change the date into an interval of date? evt_c.1- read.table(text= patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 1 2010-12 1 2011-1 1 2011-2 2 2010-5 2 2010-6 2 2010-7 3 2010-1 3 2010-2 3 2010-4 3 2010-5 4 2011-01 4 2011-03 5 2012-04 5 2012-06 ,sep=,header=TRUE, stringsAsFactors=FALSE) mutate(evt_c.11,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x) patient_id responsed_at t 1 1 2010-5 0 2 1 2010-7 2 3 1 2010-8 3 4 1 2010-9 4 5 1 2010-12 7 6 1 2011-1 -4 7 1 2011-2 -3 8 2 2010-5 0 9 2 2010-6 1 10 2 2010-7 2 11 3 2010-1 0 12 3 2010-2 1 13 3 2010-4 3 14 3 2010-5 4 15 4 2011-01 0 16 4 2011-03 2 17 5 2012-04 0 18 5 2012-06 2 This is my problem. 2013/4/17 arun smartpink...@yahoo.com If this is not what your problem, please provide a dataset like below and explain where is the problem? - Original Message - From: arun smartpink...@yahoo.com To: GUANGUAN LUO guanguan...@gmail.com Cc: Sent: Wednesday, April 17, 2013 9:17 AM Subject: Re: how to change the date into an interval of date? Hi, I am not sure I understand your question: evt_c.1- read.table(text= patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 2 2010-5 2 2010-6 2 2010-7 3 2010-1 3 2010-2 3 2010-4 3 2010-5 4 2011-01 4 2011-03 5 2012-04 5 2012-06 ,sep=,header=TRUE,stringsAsFactors=FALSE) mutate(evt_c.1,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x) patient_id responsed_at t 1 1 2010-5 0 2 1 2010-7 2 3
Re: [R] how to change the date into an interval of date?
Hi. No problem. cc:ing to Rhelp. A.K. From: GUANGUAN LUO guanguan...@gmail.com Sent: Wednesday, April 17, 2013 10:25 AM Subject: Re: how to change the date into an interval of date? Thank you so much . That is exactly the things i want. GG Hi, Try this: library(mondate) mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],-01,sep=,patient_id,FUN=function(x) c(0,cumsum(diff(x) # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 1 2010-12 7 #6 1 2011-1 8 #7 1 2011-2 9 #8 2 2010-5 0 #9 2 2010-6 1 #10 2 2010-7 2 #11 3 2010-1 0 #12 3 2010-2 1 #13 3 2010-4 3 #14 3 2010-5 4 #15 4 2011-01 0 #16 4 2011-03 2 #17 5 2012-04 0 #18 5 2012-06 2 If it change: evt_c.1$responsed_at[6:7]- c(2011-05,2011-07) mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],-01,sep=,patient_id,FUN=function(x) c(0,cumsum(diff(x) # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 1 2010-12 7 #6 1 2011-05 12 #7 1 2011-07 14 #8 2 2010-5 0 #9 2 2010-6 1 #10 2 2010-7 2 #11 3 2010-1 0 #12 3 2010-2 1 #13 3 2010-4 3 #14 3 2010-5 4 #15 4 2011-01 0 #16 4 2011-03 2 #17 5 2012-04 0 #18 5 2012-06 2 A.K. From: GUANGUAN LUO guanguan...@gmail.com Sent: Wednesday, April 17, 2013 9:25 AM Subject: Re: how to change the date into an interval of date? mutate(evt_c.11,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x) patient_id responsed_at t 1 1 2010-5 0 2 1 2010-7 2 3 1 2010-8 3 4 1 2010-9 4 5 1 2010-12 7 6 1 2011-1 8 7 1 2011-2 9 8 2 2010-5 0 9 2 2010-6 1 10 2 2010-7 2 11 3 2010-1 0 12 3 2010-2 1 13 3 2010-4 3 14 3 2010-5 4 15 4 2011-01 0 16 4 2011-03 2 17 5 2012-04 0 18 5 2012-06 2 this is the order i want. you are so kind-hearted. GG Alright, Sorry, I misunderstood. So, what do you want your result to be at 2011-1. Is it 0? From: GUANGUAN LUO guanguan...@gmail.com Sent: Wednesday, April 17, 2013 9:21 AM Subject: Re: how to change the date into an interval of date? evt_c.1- read.table(text= patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 1 2010-12 1 2011-1 1 2011-2 2 2010-5 2 2010-6 2 2010-7 3 2010-1 3 2010-2 3 2010-4 3 2010-5 4 2011-01 4 2011-03 5 2012-04 5 2012-06 ,sep=,header=TRUE, stringsAsFactors=FALSE) mutate(evt_c.11,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x) patient_id responsed_at t 1 1 2010-5 0 2 1 2010-7 2 3 1 2010-8 3 4 1 2010-9 4 5 1 2010-12 7 6 1 2011-1 -4 7 1 2011-2 -3 8 2 2010-5 0 9 2 2010-6 1 10 2 2010-7 2 11 3 2010-1 0 12 3 2010-2 1 13 3 2010-4 3 14 3 2010-5 4 15 4 2011-01 0 16 4 2011-03 2 17 5 2012-04 0 18 5 2012-06 2 This is my problem. If this is not what your problem, please provide a dataset like below and explain where is the problem? - Original Message - To: GUANGUAN LUO guanguan...@gmail.com Cc: Sent: Wednesday, April 17, 2013 9:17 AM Subject: Re: how to change the date into an interval of date? Hi, I am not sure I understand your question: evt_c.1- read.table(text= patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 2 2010-5 2 2010-6 2 2010-7 3 2010-1 3 2010-2 3 2010-4 3 2010-5 4 2011-01 4 2011-03 5 2012-04 5 2012-06 ,sep=,header=TRUE,stringsAsFactors=FALSE) mutate(evt_c.1,t=ave(as.numeric(gsub(.*\\-,,responsed_at)),patient_id,FUN=function(x)
Re: [R] how to change the date into an interval of date?
Hi, Please check your dput(). By using your dput() output, I am getting: $patient_id [1] 2 2 2 2 3 3 3 3 $responsed_at [1] 14755 14797 14835 14883 14755 14789 14826 14857 $number [1] 1 2 3 4 1 2 3 4 $score [1] 1 1 2 3 1 5 4 5 $.Names [1] patient_id responsed_at number scores $row.names [1] NA -8 $class [1] data.frame It looks like the image from shared also showed the same output. I am not using RStudio. So, I don't know what is wrong. #the dput should be:dat1- structure(list(patient_id = c(2,2,2,2,3,3,3,3), responsed_at = c(14755,14797,14835,14883,14755,14789,14826,14857), number = c(1,2,3,4,1,2,3,4), score=c(1,1,2,3,1,5,4,5)), .Names = c(patient_id,responsed_at, number, scores),row.names=c(NA,-8L),class = data.frame) dat1 # patient_id responsed_at number scores #1 2 14755 1 1 #2 2 14797 2 1 #3 2 14835 3 2 #4 2 14883 4 3 #5 3 14755 1 1 #6 3 14789 2 5 #7 3 14826 3 4 #8 3 14857 4 5 library(zoo) dat1$responsed_at-as.Date(dat1$responsed_at) dat1 # patient_id responsed_at number scores #1 2 2010-05-26 1 1 #2 2 2010-07-07 2 1 #3 2 2010-08-14 3 2 #4 2 2010-10-01 4 3 #5 3 2010-05-26 1 1 #6 3 2010-06-29 2 5 #7 3 2010-08-05 3 4 #8 3 2010-09-05 4 5 str(dat1) #'data.frame': 8 obs. of 4 variables: # $ patient_id : num 2 2 2 2 3 3 3 3 # $ responsed_at: Date, format: 2010-05-26 2010-07-07 ... # $ number : num 1 2 3 4 1 2 3 4 # $ scores : num 1 1 2 3 1 5 4 5 A.K. From: GUANGUAN LUO guanguan...@gmail.com To: arun smartpink...@yahoo.com Sent: Tuesday, April 16, 2013 10:49 AM Subject: Re: how to change the date into an interval of date? hi, dput(head(data,8)) structure(list(patient_id = c(2,2,2,2,3,3,3,3), responsed_at = c(14755,14797,14835,14883,14755,14789,14826,14857), number = c(1,2,3,4,1,2,3,4), score=c(1,1,2,3,1,5,4,5), .Names = c(patient_id, responsed_at, number, scores), class = data.frame)) like this? i use R studio, there are 4 windows , window of results is the output. 2013/4/16 arun smartpink...@yahoo.com HI, Please dput() your dataset as in my previous reply. This is image and it is twice or thrice the work for me to convert it to readable form. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Also, you didn't answer to my question: I didn't understand which one is window of results and which is my tables. From: GUANGUAN LUO guanguan...@gmail.com To: smartpink...@yahoo.com Sent: Tuesday, April 16, 2013 10:10 AM Subject: Re: how to change the date into an interval of date? patient_id number response_id session_id responsed_at login clinique_basdai.fatigue 1 2 1 77 2 14755 3002 4 2 2 2 1258 61 14797 3002 5 3 2 3 2743 307 14835 3002 5 4 2 4 4499 562 14883 3002 6 5 2 5 6224 809 14916 3002 4 6 2 6 7708 1024 14949 3002 3 7 2 7 9475 1224 14985 3002 3 8 2 8 11362 1458 15020 3002 4 9 2 9 13417 1688 15055 3002 5 10 2 10 15365 1959 15090 3002 4 11 2 11 17306 2211 15126 3002 5 12 2 12 19073 2449 15160 3002 3 13 2 13 20679 2677 15193 3002 5 14 2 14 22294 2883 15228 3002 5 15 2 15 24097 3082 15265 3002 5 16 2 16 25670 3304 15299 3002 5
Re: [R] how to change the date into an interval of date?
Hi, I am not sure I understand your question correctly. dat1- read.table(text= id responsed_at number_of_connection scores 1 12-01-2010 1 2 1 15-02-2010 2 3 1 01-04-2010 3 2 ,sep=,header=TRUE,stringsAsFactors=FALSE) datNew- dat1 datNew$responsed_at- gsub(^.*\\-(.*\\-.*),\\1, datNew$responsed_at) dat2-data.frame(responsed_at=format(seq(from=as.Date(01-01-2010,format=%d-%m-%Y),to=as.Date(01-04-2010,format=%d-%m-%Y),by=month),%m-%Y)) res-merge(datNew,dat2,all=TRUE,by=responsed_at) res$responsed_at[is.na(res$id)]-NA res$responsed_at[!is.na(res$responsed_at)]- paste(gsub((.*)\\-.*\\-.*,\\1,dat1$responsed_at),-,res$responsed_at[!is.na(res$responsed_at)],sep=) res$number_of_month- seq_len(nrow(res))-1 res # responsed_at id number_of_connection scores number_of_month #1 12-01-2010 1 1 2 0 #2 15-02-2010 1 2 3 1 #3 NA NA NA NA 2 #4 01-04-2010 1 3 2 3 Dear experts: I have my table like this: id responsed_at number of connection scores 1 12-01-2010 1 2 1 15-02-2010 2 3 1 01-04-2010 3 2 I want to have one table like this: id responsed_at number of month number of connection scors 1 12-01-2010 0 1 2 1 15-02-2010 1 2 3 1 NA 2 NA NA 1 01-04-2010 3 3 2 I want to plus a column indicating the number of month when patients responsed. How can I realize that in R? The rule for number of month is the date in one month, ex: from the first day to the last day of one month count one number of month. Thank you in avance. __ R-help@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.