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
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 17 days ago.
Improve this question
I have a dataset with time, where the time intervals are 6 hours apart and I have a column of heaterstatus.
The dataset :
I would like to know the percentage of zero occurred in each day for heaterstatus. New to R, any suggestion will be helpful.
Not tested since you only provided data as an image, but this should do what you want:
library(dplyr)
dat %>%
group_by(day = as.Date(Time)) %>%
summarize(pct_0 = mean(HeaterStatus == 0))
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
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))
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)
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"