How to create new column that compares 2 other columns [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 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

Related

How to find the percentage of observations in each day in R? [closed]

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))

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)

How to check for a particular data of one data frame in some other data frame 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 5 years ago.
Improve this question
I have two data frames called df1 and df2, and df1 has 2 columns named poi, score. And the another data frame df2 has only one column called poi_ and it contains a few common data from df1$poi. I would be needing to check which df2$poi_ have their score defined in df1$poi and if score is present then put a new column called score_ in df2 and fill the column with the score found in df1
try this:
res <- merge(df2,df1,by.x="poi_",by.y="poi")
names(res)[2] <- "score_"

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