R - Stuck in a IF Else [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 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))

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

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)

Transform condition to numeric 0 1 [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 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)

filtering R dataframe and show each variable once [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 5 years ago.
Improve this question
I have a huge dataframe that contains a column of gene's IDs. Each Gene ID appears in the column in a different number of times.
I want to extract from the dataframe a column that presents every Gene ID once, and at the same time I want to keep the data as a dataframe and not to change it to a list with factors.
example:
GeneID
589034
489034
589034
589034
48999
99449
99449
And i want my output to be:
GeneID
589034
489034
48999
99449
You can use the unique function for this:
dat = c('GeneID', '589034', '489034', '589034', '589034', '48999', '99449', '99449')
unique(dat)
[1] "GeneID" "589034" "489034" "48999" "99449"

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