Re: [R] Replace / with - in date

2010-04-16 Thread arnaud Gaboury
Why don't you try something like :

Xd$x=as.date(xd$x,format=%y/%m/%d).





 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Christian Raschke
 Sent: Thursday, April 15, 2010 8:28 PM
 To: r-help@r-project.org
 Subject: Re: [R] Replace / with - in date
 
 Is there anything that speaks against just applying gsub to the factor
 levels if one would like to keep everything as factors (and not
 consider
 true Date classes or character vectors)? I.e:
 
   x - c(2000/01/01, 2001/02/01)
   xd - as.data.frame(x)
   levels(xd$x) - gsub(/, -, levels(xd$x))
 
 Christian
 
 
 On 04/15/2010 01:08 PM, David Winsemius wrote:
 
  On Apr 15, 2010, at 1:51 PM, prem_R wrote:
 
 
  Hi,every one .I have searched the solutions in the forum  for
  replacing my
  date value which is in a data frame  ,01/01/2000 to 01-01-2000 using
  replace
  function but got the following warning message
  x-2000/01/01
  xd-as.data.frame(x)
  xd$x-replace(xd$x,xd$x==/,-)
 
  The replace function does not work with factors, it works with
  (complete) vectors, not substrings. It's also a real hassle to do
 such
  operations on factors, so just use character vectors and try gsub
  instead:
 
   x-2000/01/01
   xd-as.data.frame(x, stringsAsFactors=FALSE)
   xd$x2-gsub(/,-, xd$x)
   xd
 x x2
  1 2000/01/01 2000-01-01
 
 
 
  Warning message:
  In `[-.factor`(`*tmp*`, list, value = -) :
   invalid factor level, NAs generated
 
  Is there any other method of doing it? or am i missing something?.
  please
  let me know if you need any more information.
 
  Thanks.
 
  Prem
  --
  View this message in context:
  http://n4.nabble.com/Replace-with-in-date-tp1911391p1911391.html
  Sent from the R help mailing list archive at Nabble.com.
 
  __
  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.
 
  David Winsemius, MD
  West Hartford, CT
 
  __
  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.
 
 
 --
 Christian Raschke
 Department of Economics
 and
 ISDS Research Lab (HSRG)
 Louisiana State University
 Patrick Taylor Hall, Rm 2128
 Baton Rouge, LA 70803
 cras...@lsu.edu
 
 __
 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.

__
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.


[R] Replace / with - in date

2010-04-15 Thread prem_R

Hi,every one .I have searched the solutions in the forum  for replacing my
date value which is in a data frame  ,01/01/2000 to 01-01-2000 using replace
function but got the following warning message
x-2000/01/01
 xd-as.data.frame(x)
xd$x-replace(xd$x,xd$x==/,-)


Warning message:
In `[-.factor`(`*tmp*`, list, value = -) :
  invalid factor level, NAs generated

Is there any other method of doing it? or am i missing something?. please
let me know if you need any more information.

Thanks.

Prem
-- 
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1911391.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Replace / with - in date

2010-04-15 Thread David Winsemius


On Apr 15, 2010, at 1:51 PM, prem_R wrote:



Hi,every one .I have searched the solutions in the forum  for  
replacing my
date value which is in a data frame  ,01/01/2000 to 01-01-2000 using  
replace

function but got the following warning message
x-2000/01/01
xd-as.data.frame(x)
xd$x-replace(xd$x,xd$x==/,-)


The replace function does not work with factors, it works with  
(complete) vectors, not substrings. It's also a real hassle to do such  
operations on factors, so just use character vectors and try gsub  
instead:


 x-2000/01/01
 xd-as.data.frame(x, stringsAsFactors=FALSE)
 xd$x2-gsub(/,-, xd$x)
 xd
   x x2
1 2000/01/01 2000-01-01




Warning message:
In `[-.factor`(`*tmp*`, list, value = -) :
 invalid factor level, NAs generated

Is there any other method of doing it? or am i missing something?.  
please

let me know if you need any more information.

Thanks.

Prem
--
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1911391.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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] Replace / with - in date

2010-04-15 Thread prem_R

Thanks Mr.Winsemius  for the proposed solution ,it worked fine.But when came
to the entire data frame containing 40 rows ,i used the following code
as proposed by you 

a$date1-gsub(/,-,a$date)

got the following error:
Error in `$-.data.frame`(`*tmp*`, date1, value = character(0)) : 
  replacement has 0 rows, data has 441906

Please provide solution with the problem.

Thanks.
-- 
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1914541.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Replace / with - in date

2010-04-15 Thread Christian Raschke
Is there anything that speaks against just applying gsub to the factor 
levels if one would like to keep everything as factors (and not consider 
true Date classes or character vectors)? I.e:


 x - c(2000/01/01, 2001/02/01)
 xd - as.data.frame(x)
 levels(xd$x) - gsub(/, -, levels(xd$x))

Christian


On 04/15/2010 01:08 PM, David Winsemius wrote:


On Apr 15, 2010, at 1:51 PM, prem_R wrote:



Hi,every one .I have searched the solutions in the forum  for 
replacing my
date value which is in a data frame  ,01/01/2000 to 01-01-2000 using 
replace

function but got the following warning message
x-2000/01/01
xd-as.data.frame(x)
xd$x-replace(xd$x,xd$x==/,-)


The replace function does not work with factors, it works with 
(complete) vectors, not substrings. It's also a real hassle to do such 
operations on factors, so just use character vectors and try gsub 
instead:


 x-2000/01/01
 xd-as.data.frame(x, stringsAsFactors=FALSE)
 xd$x2-gsub(/,-, xd$x)
 xd
   x x2
1 2000/01/01 2000-01-01




Warning message:
In `[-.factor`(`*tmp*`, list, value = -) :
 invalid factor level, NAs generated

Is there any other method of doing it? or am i missing something?. 
please

let me know if you need any more information.

Thanks.

Prem
--
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1911391.html

Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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.



--
Christian Raschke
Department of Economics
and
ISDS Research Lab (HSRG)
Louisiana State University
Patrick Taylor Hall, Rm 2128
Baton Rouge, LA 70803
cras...@lsu.edu

__
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] Replace / with - in date

2010-04-15 Thread David Winsemius


On Apr 15, 2010, at 2:22 PM, prem_R wrote:



Thanks Mr.Winsemius  for the proposed solution ,it worked fine.But  
when came
to the entire data frame containing 40 rows ,i used the  
following code

as proposed by you

a$date1-gsub(/,-,a$date)

got the following error:
Error in `$-.data.frame`(`*tmp*`, date1, value = character(0)) :
 replacement has 0 rows, data has 441906

Please provide solution with the problem.


Please provide sample data for the problem.



Thanks.
--
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1914541.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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] Replace / with - in date

2010-04-15 Thread David Winsemius


On Apr 15, 2010, at 2:28 PM, Christian Raschke wrote:

Is there anything that speaks against just applying gsub to the  
factor levels if one would like to keep everything as factors (and  
not consider true Date classes or character vectors)? I.e:


 x - c(2000/01/01, 2001/02/01)
 xd - as.data.frame(x)
 levels(xd$x) - gsub(/, -, levels(xd$x))


Nothing. Other than the fact (which is not at all obvious to the new  
useRs) that it requires knowledge that the string representations are  
stored within the factor levels and not the factor values, and that  
operations on factors are often quite puzzling to the uninitiated. If  
one were to attempt applying a function that takes Date or Datetime  
arguments to such a date-like-factor, it is very likely that the  
initial results would be failures. The OP referred to the strings as a  
date value rather than as a factor level. I doubt that he realized  
the default operation of as.data.frame().


--
David.



Christian


On 04/15/2010 01:08 PM, David Winsemius wrote:


On Apr 15, 2010, at 1:51 PM, prem_R wrote:



Hi,every one .I have searched the solutions in the forum  for  
replacing my
date value which is in a data frame  ,01/01/2000 to 01-01-2000  
using replace

function but got the following warning message
x-2000/01/01
xd-as.data.frame(x)
xd$x-replace(xd$x,xd$x==/,-)


The replace function does not work with factors, it works with  
(complete) vectors, not substrings. It's also a real hassle to do  
such operations on factors, so just use character vectors and try  
gsub instead:


 x-2000/01/01
 xd-as.data.frame(x, stringsAsFactors=FALSE)
 xd$x2-gsub(/,-, xd$x)
 xd
  x x2
1 2000/01/01 2000-01-01




Warning message:
In `[-.factor`(`*tmp*`, list, value = -) :
invalid factor level, NAs generated

Is there any other method of doing it? or am i missing something?.  
please

let me know if you need any more information.

Thanks.

Prem
--
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1911391.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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.



--
Christian Raschke
Department of Economics
and
ISDS Research Lab (HSRG)
Louisiana State University
Patrick Taylor Hall, Rm 2128
Baton Rouge, LA 70803
cras...@lsu.edu

__
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.


David Winsemius, MD
West Hartford, CT

__
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] Replace / with - in date

2010-04-15 Thread prem_R

This is my sample data

2000/01/01
2000/01/01
2000/02/01
2000/10/01
2000/19/01
2000/20/01
2000/21/01
2000/22/01
2000/23/01
2000/25/01
2000/26/01
2000/27/01
2000/28/01
2000/29/01

Tried using levels function but got the following error .
Error in levels(a$date) - gsub(/, -, levels(a$date)) : 
  attempt to set an attribute on NULL

Thanks.


-- 
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1934333.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Replace / with - in date

2010-04-15 Thread David Winsemius
It's going to work better if you offer a non-ambiguous version of your  
data. Use dump or dput. If you use the dump route, then see the  
Posting Guide example. If you use dput, then it is as simple as  
dput(object).


Perhaps (if dfrm is the name of a dataframe):

dput(head(dfrm))


On Apr 15, 2010, at 5:27 PM, prem_R wrote:



This is my sample data

2000/01/01


We cannot tell from the output whether that is a factor or a character  
vector.



2000/01/01
2000/02/01
2000/10/01
2000/19/01
2000/20/01
2000/21/01
2000/22/01
2000/23/01
2000/25/01
2000/26/01
2000/27/01
2000/28/01
2000/29/01

Tried using levels function but got the following error .
Error in levels(a$date) - gsub(/, -, levels(a$date)) :
 attempt to set an attribute on NULL

Thanks.


--



David Winsemius, MD
West Hartford, CT

__
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] Replace / with - in date

2010-04-15 Thread David Winsemius


On Apr 15, 2010, at 5:46 PM, David Winsemius wrote:

It's going to work better if you offer a non-ambiguous version of  
your data. Use dump or dput. If you use the dump route, then see the  
Posting Guide example. If you use dput, then it is as simple as  
dput(object).


Perhaps (if dfrm is the name of a dataframe):

dput(head(dfrm))


On Apr 15, 2010, at 5:27 PM, prem_R wrote:



This is my sample data

2000/01/01


We cannot tell from the output whether that is a factor or a  
character vector.



2000/01/01
2000/02/01
2000/10/01
2000/19/01
2000/20/01
2000/21/01
2000/22/01
2000/23/01
2000/25/01
2000/26/01
2000/27/01
2000/28/01
2000/29/01

Tried using levels function but got the following error .
Error in levels(a$date) - gsub(/, -, levels(a$date)) :
attempt to set an attribute on NULL


So it probably was not a factor variable despite what you offered in  
your previous example. The gsub function should be able to do the  
conversion if it's a character vector, as in this example:


gsub(/,-,
 read.table(textConnection(2000/01/01
 2000/01/01
 2000/02/01
 2000/10/01
 2000/19/01
 2000/20/01
 2000/21/01
 2000/22/01
 2000/23/01
 2000/25/01
 2000/26/01
 2000/27/01
 2000/28/01
 2000/29/01), header=FALSE, stringsAsFactors=FALSE)[,1] )

--- returns:
 [1] 2000-01-01 2000-01-01 2000-02-01 2000-10-01 2000-19-01  
2000-20-01
 [7] 2000-21-01 2000-22-01 2000-23-01 2000-25-01 2000-26-01  
2000-27-01

[13] 2000-28-01 2000-29-01



Thanks.


--



David Winsemius, MD
West Hartford, CT

__
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.


David Winsemius, MD
West Hartford, CT

__
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] Replace / with - in date

2010-04-15 Thread prem_R

  x - c(2000/01/01,
2001/02/01,2000/03/01,2000/04/01,2000/05/01,2000/06/01,2000/07/01,2000/08/01,2000/09/01,2000/10/01)
  xd - as.data.frame(x)
  levels(xd$x) - gsub(/, -, levels(xd$x)) 
  fix(xd)

This works fine .but with one more variable say xsd in the data frame i'm
getting the following error


Error in levels(sa$date1) - gsub(/, -, levels(sa$date)) : 
  attempt to set an attribute on NULL

-- 
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1934428.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Replace / with - in date

2010-04-15 Thread David Winsemius


On Apr 15, 2010, at 7:20 PM, prem_R wrote:




x - c(2000/01/01,

2001
/ 
02 
/ 
01 
,2000 
/ 
03 
/ 
01 
,2000 
/ 
04 
/ 
01 
,2000 
/ 
05 
/01,2000/06/01,2000/07/01,2000/08/01,2000/09/01,2000/10/01)

xd - as.data.frame(x)
levels(xd$x) - gsub(/, -, levels(xd$x))
fix(xd)


This works fine .but with one more variable say xsd in the data  
frame i'm

getting the following error


Error in levels(sa$date1) - gsub(/, -, levels(sa$date)) :
 attempt to set an attribute on NULL



Cannot reproduce the error (and you are asked in the Posting Guide to  
include the code that creates the error rather than posting code that  
does not).


x - c(2000/01/01,
2001 
/ 
02 
/ 
01 
,2000 
/ 
03 
/ 
01 
,2000 
/ 
04 
/ 
01 
,2000 
/ 
05/01,2000/06/01,2000/07/01,2000/08/01,2000/09/01,2000/10/01)

xd - data.frame(x=x, a=1)  # creates another column in a.
levels(xd$x) - gsub(/, -, levels(xd$x))
 xd
x a
1  2000-01-01 1
2  2001-02-01 1
3  2000-03-01 1
4  2000-04-01 1
5  2000-05-01 1
6  2000-06-01 1
7  2000-07-01 1
8  2000-08-01 1
9  2000-09-01 1
10 2000-10-01 1

 sessionInfo()
R version 2.10.1 RC (2009-12-09 r50695)
x86_64-apple-darwin9.8.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] splines   stats graphics  grDevices utils datasets   
methods   base


other attached packages:
[1] adapt_1.0-4 Hmisc_3.7-0 survival_2.35-7 mapdata_2.1-1
maps_2.1-0


loaded via a namespace (and not attached):
[1] cluster_1.12.1 grid_2.10.1lattice_0.18-3 tools_2.10.1




--
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1934428.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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] Replace / with - in date

2010-04-15 Thread David Winsemius


On Apr 15, 2010, at 7:20 PM, prem_R wrote:




x - c(2000/01/01,

2001
/ 
02 
/ 
01 
,2000 
/ 
03 
/ 
01 
,2000 
/ 
04 
/ 
01 
,2000 
/ 
05 
/01,2000/06/01,2000/07/01,2000/08/01,2000/09/01,2000/10/01)

xd - as.data.frame(x)
levels(xd$x) - gsub(/, -, levels(xd$x))
fix(xd)


This works fine .but with one more variable say xsd in the data  
frame i'm

getting the following error


Error in levels(sa$date1) - gsub(/, -, levels(sa$date)) :
 attempt to set an attribute on NULL


Are there two columns date1 and  date in sa? Have you made a typo?


--
View this message in context: 
http://n4.nabble.com/Replace-with-in-date-tp1911391p1934428.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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.