How can I calculate a new column for a dataframe in R based on another dataframe?

Advertisements I’m having trouble adding a new column to a dataframe based on values from another dataframe. I’m going to try to provide a minimal, replicable example; of course my real dataframes have lots of other columns, but I don’t think they matter here. So let’s say I have two dataframes, DF1 and DF2. ID… Read More How can I calculate a new column for a dataframe in R based on another dataframe?

Filling in Missing Values in R Based on Previous Rows within Group

Advertisements I have a dataset in long-format called df containing four key variables: respondent_id, time (the time period), change (a variable indicating the last period before a change of income) and lost_income, a variable indicating when a person lost their income. The lost_income column is currently empty, only in rows that follow a row where… Read More Filling in Missing Values in R Based on Previous Rows within Group

How to replace repeated measurements with the mean of the first three or two measurements in R

Advertisements This is a slight variation on a question I asked previously, which I have not been able to adapt the answer for. When there are repeated measurements I would like to replace the associated BP/BP2 with the mean for the first three measurements (if >=3), or the mean of two measurements (if == 2).… Read More How to replace repeated measurements with the mean of the first three or two measurements in R

Make new colum based on values in two different columns by group

Advertisements I want make a ‘new column’ with values from ‘number’ only for ‘sp.name’ (grouping variable) where both responses ‘young’ and ‘adult’ are present; if not, enter 0 in the ‘new column’. df <- data.frame(sp.name= c(‘a’,’a’, ‘b’, ‘b’ ,’c’, ‘d’ ), number=c(2,2,3, 3,4,4), stage= c(‘adult’, ‘young’, ‘young’,’adult’, ‘adult’, ‘young’)) Here is what I tried. df… Read More Make new colum based on values in two different columns by group

Using quoted variable names when creating functions containing ellipses (i.e. '…') in dplyr

Advertisements I am trying to learn how to use the ellipses (…) when I program using dplyr. I cannot work out how to pass a character into the ellipses. Here is a toy problem to illustrate set.seed(10) data.frame(var1 = factor(sample(x = letters[1:3], size = 10, replace = T))) -> df Now say I want to… Read More Using quoted variable names when creating functions containing ellipses (i.e. '…') in dplyr