Transform condition to numeric 0 1 [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My datasheet d contain multiple covariates regarding a specific disease. One of them is "Age on diagnosis", which is coded as d$Age.
I want to make a new variable called "Age10" where Age < 10 (age below 10 years on time of diagnosis) is coded 0 and Age >= (age equal to or higher on time of diagnosis) is coded as 1.
I have tried subsetting without succes
Can you help?

The below should work:
d$Age10 <- as.numeric(d$Age >= 10)

Related

How to create new column that compares 2 other columns [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have dataset in R for samples (ID) for 2 years for one variable (Majorclade). I want to see how major clade have changed over the 2 years for each sample. I would like to create a column that compares it, like it is the same calls it 0, if different calls it 1. I imagine some kinda of mutate would do it, but I am not figuring it out. Ideas?
Table example:
We can use
library(dplyr)
df1 %>%
group_by(ID) %>%
mutate(new = +(n_distinct(Majorclade) > 1)) %>%
ungroup

How can I compare and represent the categorical data of my two columns with 0 if the rows are the same and 1 if not, using R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How can I compare the categorical data of my two columns and visualize it with a 0 if the row is the same for the two columns and if different 1.
If the categorical columns of your dataframe are named x and y and have the same levels you can assign a new column to your dataframe with the result of comparing the two columns using the ifelse function.
df$match <- ifelse(df$x == df$y, 0, 1)

R - Stuck in a IF Else [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am stuck in an exercise that askes me to read a data frame and write an ifelse statement that returns 1 if the sex (theres a gender column) is Female and 2 if the sex is Male. Then the exercise askes me the sum() of theses numbers. No success so far. Any help?
This should work.
## define data frame
df <- data.frame(
id=c(1,2,3,4,5),
gender=c("Male","Female","Male","Female","Male")
)
## male=1,female=2
sum(ifelse(df$gender == "Male",1,2))

R: How to recode multiple values in dplyr? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How can I make the following changes for multiple or all variables?
change "yes" to 1
change "no" to 0
keep NAs
I tried recode but it seems not to be appliable to dataframes.
x <- data.frame(y=sample(c("yes", "no", "NA"), 10, replace = TRUE))
library(tidyr)
x$y2<- recode_factor(x$y, yes=1, no=0)

Calculate % change in each observation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to calculate the % change in values for Argentina for the entire column and store it in a data frame:
% change is 2nd value- 1st value/ 1st value *100
say if a column has
30
40
%change is 40-30/30 =33.33%
Pls read abot how to make a reproducible example
Supposing df is your data.frame. Using dplyr:
library(dplyr)
df %>%
mutate(change = (Argentina - lead(Argentina)) / Argentina * 100

Resources