CALUM POLWART
2023-Nov-04 23:14 UTC
[R] I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
I might have factored the gender.
I'm not sure it would in any way be quicker. But might be to some extent
easier to develop variations of. And is sort of what factors should be
doing...
# make dummy data
gender <- c("Male", "Female", "Male",
"Female")
WC <- c(70,60,75,65)
TG <- c(0.9, 1.1, 1.2, 1.0)
myDf <- data.frame( gender, WC, TG )
# label a factor
myDf$GF <- factor(myDf$gender, labels= c("Male"=65,
"Female"=58))
# do the maths
myDf$LAP <- (myDf$WC - as.numeric(myDf$GF))* myDf$TG
#show results
head(myDf)
gender WC TG GF LAP
1 Male 70 0.9 58 61.2
2 Female 60 1.1 65 64.9
3 Male 75 1.2 58 87.6
4 Female 65 1.0 65 64.0
(Reality: I'd have probably used case_when in tidy to create a new numeric
column)
The equation to> calculate LAP is different for male and females. I am giving both equations
> below.
>
> LAP for male = (WC-65)*TG
> LAP for female = (WC-58)*TG
>
> My question is 'how can I calculate the LAP and create a single new
column?
>
>
[[alternative HTML version deleted]]
@vi@e@gross m@iii@g oii gm@ii@com
2023-Nov-05 04:27 UTC
[R] I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
There are many techniques Callum and yours is an interesting twist I had not
considered.
Yes, you can specify what integer a factor uses to represent things but not what
I meant. Of course your trick does not work for some other forms of data like
real numbers in double format. There is a cost to converting a column to a
factor that is recouped best if it speeds things up multiple times.
The point I was making was that when you will be using group_by, especially if
done many times, it might speed things up if the column is already a normal
factor, perhaps just indexed from 1 onward. My guess is that underneath the
covers, some programs implicitly do such a factor conversion if needed. An
example might be aspects of the ggplot program where you may get a mysterious
order of presentation in the graph unless you create a factor with the order you
wish to have used and avoid it making one invisibly.
From: CALUM POLWART <polc1410 at gmail.com>
Sent: Saturday, November 4, 2023 7:14 PM
To: avi.e.gross at gmail.com
Cc: Jorgen Harmse <JHarmse at roku.com>; r-help at r-project.org;
mkzaman.m at gmail.com
Subject: Re: [R] I need to create new variables based on two numeric variables
and one dichotomize conditional category variables.
I might have factored the gender.
I'm not sure it would in any way be quicker. But might be to some extent
easier to develop variations of. And is sort of what factors should be doing...
# make dummy data
gender <- c("Male", "Female", "Male",
"Female")
WC <- c(70,60,75,65)
TG <- c(0.9, 1.1, 1.2, 1.0)
myDf <- data.frame( gender, WC, TG )
# label a factor
myDf$GF <- factor(myDf$gender, labels= c("Male"=65,
"Female"=58))
# do the maths
myDf$LAP <- (myDf$WC - as.numeric(myDf$GF))* myDf$TG
#show results
head(myDf)
gender WC TG GF LAP
1 Male 70 0.9 58 61.2
2 Female 60 1.1 65 64.9
3 Male 75 1.2 58 87.6
4 Female 65 1.0 65 64.0
(Reality: I'd have probably used case_when in tidy to create a new numeric
column)
The equation to
calculate LAP is different for male and females. I am giving both equations
below.
LAP for male = (WC-65)*TG
LAP for female = (WC-58)*TG
My question is 'how can I calculate the LAP and create a single new column?
[[alternative HTML version deleted]]
Jorgen Harmse
2023-Nov-06 16:22 UTC
[R] [EXTERNAL] Re: I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
That?s ingenious, but I would hesitate to rely on a specific mapping between
strings and integers. (I usually read data frames with stringsAsFactors=FALSE or
coerce to character later: I don?t think it takes more memory.) Maybe create
another column with the coefficients. What if gender is part of another formula?
Regards,
Jorgen Harmse.
From: CALUM POLWART <polc1410 at gmail.com>
Date: Saturday, November 4, 2023 at 18:23
To: avi.e.gross at gmail.com <avi.e.gross at gmail.com>
Cc: Jorgen Harmse <JHarmse at roku.com>, r-help at r-project.org
<r-help at r-project.org>, mkzaman.m at gmail.com <mkzaman.m at
gmail.com>
Subject: [EXTERNAL] Re: [R] I need to create new variables based on two numeric
variables and one dichotomize conditional category variables.
I might have factored the gender.
I'm not sure it would in any way be quicker. But might be to some extent
easier to develop variations of. And is sort of what factors should be doing...
# make dummy data
gender <- c("Male", "Female", "Male",
"Female")
WC <- c(70,60,75,65)
TG <- c(0.9, 1.1, 1.2, 1.0)
myDf <- data.frame( gender, WC, TG )
# label a factor
myDf$GF <- factor(myDf$gender, labels= c("Male"=65,
"Female"=58))
# do the maths
myDf$LAP <- (myDf$WC - as.numeric(myDf$GF))* myDf$TG
#show results
head(myDf)
gender WC TG GF LAP
1 Male 70 0.9 58 61.2
2 Female 60 1.1 65 64.9
3 Male 75 1.2 58 87.6
4 Female 65 1.0 65 64.0
(Reality: I'd have probably used case_when in tidy to create a new numeric
column)
The equation to
calculate LAP is different for male and females. I am giving both equations
below.
LAP for male = (WC-65)*TG
LAP for female = (WC-58)*TG
My question is 'how can I calculate the LAP and create a single new column?
[[alternative HTML version deleted]]
Apparently Analagous Threads
- I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
- I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
- [EXTERNAL] RE: I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
- I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
- I need to create new variables based on two numeric variables and one dichotomize conditional category variables.