Issue with Transform function in R

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue with Transform function in R

Bhaskar Mitra
Hello Everyone,


I am trying to replace the values in the 2nd column (Variable 1)
corresponding to certain dates  (Date)


with NAs as shown below. Both Date and Variable1 are numeric vectors . I am
trying to use the transform function


as shown below but it doesn’t seem to work even though if I am not getting
any error


Any suggestions/help in this regard?

regards,

---------------------------------------------------------------------------------------------------------------------------------------------------------



Df1 <- data.frame(Date, Variable1)




a1 <- transform(Df1, ifelse(Date  > "010301000300 " && Date <
"010501000300", Variable1 ==NA, Variable1))


Original Data frame



     Date                          Variable1

010101000300                     1

010201000300                     2

010301000300                     3

010401000300                     4

010501000300                     5

010601000300                     6

010701000300                     7

 .

.

.

……….




Transformed data frame (i hope to transform)



      Date                       Variable1

010101000300                    1

010201000300                    2

010301000300                   NA

010401000300                   NA

010501000300                    NA

010601000300                    6

010701000300                    7



……….

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Issue with Transform function in R

Ivan Calandra-4
This might not be the whole story, but part of the problem is that you
want to select a _*character string*_ greater/smaller than another. That
doesn't make much sense!

I am not sure how to best compare two dates, but if you convert the Date
values into numeric, then that would work. The problem is that it seems
your Date values are character, and the comparison in your ifelse is
also a character.

So something like this might work (untested because no reproducible
example):
transform(Df1, ifelse(as.numeric(Date) > numeric.value1 &&
as.numeric(Date) < numeric.value2, Variable1 ==NA, Variable1))

HTH,
Ivan

--
Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 21/07/2016 à 03:00, Bhaskar Mitra a écrit :

> Hello Everyone,
>
>
> I am trying to replace the values in the 2nd column (Variable 1)
> corresponding to certain dates  (Date)
>
>
> with NAs as shown below. Both Date and Variable1 are numeric vectors . I am
> trying to use the transform function
>
>
> as shown below but it doesn’t seem to work even though if I am not getting
> any error
>
>
> Any suggestions/help in this regard?
>
> regards,
>
> ---------------------------------------------------------------------------------------------------------------------------------------------------------
>
>
>
> Df1 <- data.frame(Date, Variable1)
>
>
>
>
> a1 <- transform(Df1, ifelse(Date  > "010301000300 " && Date <
> "010501000300", Variable1 ==NA, Variable1))
>
>
> Original Data frame
>
>
>
>       Date                          Variable1
>
> 010101000300                     1
>
> 010201000300                     2
>
> 010301000300                     3
>
> 010401000300                     4
>
> 010501000300                     5
>
> 010601000300                     6
>
> 010701000300                     7
>
>   .
>
> .
>
> .
>
> ……….
>
>
>
>
> Transformed data frame (i hope to transform)
>
>
>
>        Date                       Variable1
>
> 010101000300                    1
>
> 010201000300                    2
>
> 010301000300                   NA
>
> 010401000300                   NA
>
> 010501000300                    NA
>
> 010601000300                    6
>
> 010701000300                    7
>
>
>
> ……….
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Issue with Transform function in R

Rui Barradas
Hello,

Another thing to consider is to use Variable1 = NA, not '=='.
With '==' it will probably return TRUE/FALSE/NA.

Hope this helps,

Rui Barradas
 

Citando Ivan Calandra <[hidden email]>:

> This might not be the whole story, but part of the problem is that  
> you want to select a _*character string*_ greater/smaller than  
> another. That doesn't make much sense!
>
> I am not sure how to best compare two dates, but if you convert the  
> Date values into numeric, then that would work. The problem is that  
> it seems your Date values are character, and the comparison in your  
> ifelse is also a character.
>
> So something like this might work (untested because no reproducible example):
> transform(Df1, ifelse(as.numeric(Date) > numeric.value1 &&  
> as.numeric(Date) < numeric.value2, Variable1 ==NA, Variable1))
>
> HTH,
> Ivan
>
> --
> Ivan Calandra, PhD
> Scientific Mediator
> University of Reims Champagne-Ardenne
> GEGENAA - EA 3795
> CREA - 2 esplanade Roland Garros
> 51100 Reims, France
> +33(0)3 26 77 36 89
> [hidden email]
> --
> https://www.researchgate.net/profile/Ivan_Calandra
> https://publons.com/author/705639/
>
> Le 21/07/2016 à 03:00, Bhaskar Mitra a écrit :
>> Hello Everyone,
>>
>> I am trying to replace the values in the 2nd column (Variable 1)
>> corresponding to certain dates  (Date)
>>
>> with NAs as shown below. Both Date and Variable1 are numeric vectors . I am
>> trying to use the transform function
>>
>> as shown below but it doesn’t seem to work even though if I am not getting
>> any error
>>
>> Any suggestions/help in this regard?
>>
>> regards,
>>
>> ---------------------------------------------------------------------------------------------------------------------------------------------------------
>>
>> Df1 <- data.frame(Date, Variable1)
>>
>> a1 <- transform(Df1, ifelse(Date  > "010301000300 " && Date <
>> "010501000300", Variable1 ==NA, Variable1))
>>
>> Original Data frame
>>
>>      Date                          Variable1
>>
>> 010101000300                     1
>>
>> 010201000300                     2
>>
>> 010301000300                     3
>>
>> 010401000300                     4
>>
>> 010501000300                     5
>>
>> 010601000300                     6
>>
>> 010701000300                     7
>>
>> .
>>
>> .
>>
>> .
>>
>> ……….
>>
>> Transformed data frame (i hope to transform)
>>
>>       Date                       Variable1
>>
>> 010101000300                    1
>>
>> 010201000300                    2
>>
>> 010301000300                   NA
>>
>> 010401000300                   NA
>>
>> 010501000300                    NA
>>
>> 010601000300                    6
>>
>> 010701000300                    7
>>
>> ……….
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide  
> http://www.R-project.org/posting-guide.htmland provide commented,  
> minimal, self-contained, reproducible code.

 

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Issue with Transform function in R

Ivan Calandra-4
Oops, missed that one!

--
Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 21/07/2016 à 14:33, [hidden email] a écrit :

>
> Hello,
>
> Another thing to consider is to use Variable1 = NA, not '=='.
> With '==' it will probably return TRUE/FALSE/NA.
>
> Hope this helps,
>
> Rui Barradas
>
> Citando Ivan Calandra <[hidden email]
> <mailto:[hidden email]>>:
>
>> This might not be the whole story, but part of the problem is that
>> you want to select a _*character string*_ greater/smaller than
>> another. That doesn't make much sense!
>>
>> I am not sure how to best compare two dates, but if you convert the
>> Date values into numeric, then that would work. The problem is that
>> it seems your Date values are character, and the comparison in your
>> ifelse is also a character.
>>
>> So something like this might work (untested because no reproducible
>> example):
>> transform(Df1, ifelse(as.numeric(Date) > numeric.value1 &&
>> as.numeric(Date) < numeric.value2, Variable1 ==NA, Variable1))
>>
>> HTH,
>> Ivan
>>
>> --
>> Ivan Calandra, PhD
>> Scientific Mediator
>> University of Reims Champagne-Ardenne
>> GEGENAA - EA 3795
>> CREA - 2 esplanade Roland Garros
>> 51100 Reims, France
>> +33(0)3 26 77 36 89
>> [hidden email] <mailto:[hidden email]>
>> --
>> https://www.researchgate.net/profile/Ivan_Calandra
>> https://publons.com/author/705639/
>>
>> Le 21/07/2016 à 03:00, Bhaskar Mitra a écrit :
>>
>>> Hello Everyone,
>>>
>>>
>>> I am trying to replace the values in the 2nd column (Variable 1)
>>> corresponding to certain dates  (Date)
>>>
>>>
>>> with NAs as shown below. Both Date and Variable1 are numeric vectors
>>> . I am
>>> trying to use the transform function
>>>
>>>
>>> as shown below but it doesn’t seem to work even though if I am not
>>> getting
>>> any error
>>>
>>>
>>> Any suggestions/help in this regard?
>>>
>>> regards,
>>>
>>> ---------------------------------------------------------------------------------------------------------------------------------------------------------
>>>
>>>
>>>
>>> Df1 <- data.frame(Date, Variable1)
>>>
>>>
>>>
>>>
>>> a1 <- transform(Df1, ifelse(Date  > "010301000300 " && Date <
>>> "010501000300", Variable1 ==NA, Variable1))
>>>
>>>
>>> Original Data frame
>>>
>>>
>>>
>>>      Date                          Variable1
>>>
>>> 010101000300                     1
>>>
>>> 010201000300                     2
>>>
>>> 010301000300                     3
>>>
>>> 010401000300                     4
>>>
>>> 010501000300                     5
>>>
>>> 010601000300                     6
>>>
>>> 010701000300                     7
>>>
>>> .
>>>
>>> .
>>>
>>> .
>>>
>>> ……….
>>>
>>>
>>>
>>>
>>> Transformed data frame (i hope to transform)
>>>
>>>
>>>
>>>       Date                       Variable1
>>>
>>> 010101000300                    1
>>>
>>> 010201000300                    2
>>>
>>> 010301000300                   NA
>>>
>>> 010401000300                   NA
>>>
>>> 010501000300                    NA
>>>
>>> 010601000300                    6
>>>
>>> 010701000300                    7
>>>
>>>
>>>
>>> ……….
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] <mailto:[hidden email]> mailing list --
>>> To UNSUBSCRIBE and more, see
>>> 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.
>>>
>> ______________________________________________
>> [hidden email] <mailto:[hidden email]> mailing list -- To
>> UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.htmland provide commented,
>> minimal, self-contained, reproducible code.
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Issue with Transform function in R

Tom Wright-9
In reply to this post by Bhaskar Mitra
Not sure I can translate the format of your Date column correctly, however the command
DF1$Date <- as.Date(DF1$Date, format=’formatstr’)

Will convert the dates into a format correctly handled by R.
?strptime

Should give you an idea of what formatstr should look like.
I.e. if
date = 160721
as.Date(date, format=”%y%M%d”)



From: Bhaskar Mitra
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.