Creatng new variable based upon conditions

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

Creatng new variable based upon conditions

reichmaj
Given something like ...

x <- c(3,2,4,3,5,4,3,2,4,5)
y <- c("A","B","B","A","A","A","A","B","A","B")
xy <- data.frame(x,y)
xy$w <- ifelse(xy$y=="A",xy$w[,x]*10,xy$w[,x]*15 )

want to see

   x y  w
1  3 A 30
2  2 B  30
3  4 B  60
4  3 A  30
5  5 A  50
6  4 A  40
7  3 A  30
8  2 B  30
9  4 A  40
10 5 B  75

but I get NA's

Jeff

______________________________________________
[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: Creatng new variable based upon conditions

Dénes Tóth-2


On 07/26/2018 08:58 PM, JEFFERY REICHMAN wrote:
> Given something like ...
>
> x <- c(3,2,4,3,5,4,3,2,4,5)
> y <- c("A","B","B","A","A","A","A","B","A","B")
> xy <- data.frame(x,y)
> xy$w <- ifelse(xy$y=="A",xy$w[,x]*10,xy$w[,x]*15 )

You should learn the basics about how to extract or replace part of an
object, in particular data.frames. You can start by reading the help
page of ?"Extract".

xy$w <- ifelse(xy$y=="A",xy$x*10,xy$x*15 )

HTH,
Denes


>
> want to see
>
>     x y  w
> 1  3 A 30
> 2  2 B  30
> 3  4 B  60
> 4  3 A  30
> 5  5 A  50
> 6  4 A  40
> 7  3 A  30
> 8  2 B  30
> 9  4 A  40
> 10 5 B  75
>
> but I get NA's
>
> Jeff
>
> ______________________________________________
> [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: Creatng new variable based upon conditions

Bert Gunter-2
Inline.

>
>
> On 07/26/2018 08:58 PM, JEFFERY REICHMAN wrote:
>
>> Given something like ...
>>
>> x <- c(3,2,4,3,5,4,3,2,4,5)
>> y <- c("A","B","B","A","A","A","A","B","A","B")
>> xy <- data.frame(x,y)
>> xy$w <- ifelse(xy$y=="A",xy$w[,x]*10,xy$w[,x]*15 )
>>
>
> You should learn the basics about how to extract or replace part of an
> object, in particular data.frames. You can start by reading the help page
> of ?"Extract".
>

Agreed!

>
> xy$w <- ifelse(xy$y=="A",xy$x*10,xy$x*15 )
>

## or perhaps more simply:
xy$w <- with(xy, ifelse(y=="A", x*10, x*15))

See ?with

Cheers,
Bert




>
> HTH,
> Denes
>
>
>
>> want to see
>>
>>     x y  w
>> 1  3 A 30
>> 2  2 B  30
>> 3  4 B  60
>> 4  3 A  30
>> 5  5 A  50
>> 6  4 A  40
>> 7  3 A  30
>> 8  2 B  30
>> 9  4 A  40
>> 10 5 B  75
>>
>> but I get NA's
>>
>> Jeff
>>
>> ______________________________________________
>> [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/posti
>> ng-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/posti
> ng-guide.html
> and 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: Creatng new variable based upon conditions

PIKAL Petr
In reply to this post by Dénes Tóth-2
Hi

Or maybe without ifelse

xy$w <- with(xy, x * ((y != "A") + 2) * 5)

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Dénes Tóth
> Sent: Thursday, July 26, 2018 10:10 PM
> To: JEFFERY REICHMAN <[hidden email]>; [hidden email]
> Subject: Re: [R] Creatng new variable based upon conditions
>
>
>
> On 07/26/2018 08:58 PM, JEFFERY REICHMAN wrote:
> > Given something like ...
> >
> > x <- c(3,2,4,3,5,4,3,2,4,5)
> > y <- c("A","B","B","A","A","A","A","B","A","B")
> > xy <- data.frame(x,y)
> > xy$w <- ifelse(xy$y=="A",xy$w[,x]*10,xy$w[,x]*15 )
>
> You should learn the basics about how to extract or replace part of an object, in
> particular data.frames. You can start by reading the help page of ?"Extract".
>
> xy$w <- ifelse(xy$y=="A",xy$x*10,xy$x*15 )
>
> HTH,
> Denes
>
>
> >
> > want to see
> >
> >     x y  w
> > 1  3 A 30
> > 2  2 B  30
> > 3  4 B  60
> > 4  3 A  30
> > 5  5 A  50
> > 6  4 A  40
> > 7  3 A  30
> > 8  2 B  30
> > 9  4 A  40
> > 10 5 B  75
> >
> > but I get NA's
> >
> > Jeff
> >
> > ______________________________________________
> > [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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

______________________________________________
[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.