Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

Dynamic mutating of dataframe columns in R, unable to work out how to use function or loop

$
0
0

I have a R project with lots of dataframes. Each dataframe has similar naming conventions.

{For more detail - each dataframe is from a different electronic form that my field staff fill in, created in ODK and exported from server into a csv file and then imported into an R dataframe. In each electronic form you can either scan an ID barcode or if the barcode won't scan you can manually type in an ID. These go into different columns in the dataframe I want to make a new column with a single ID, regardless of whether the ID came from the "IDscan" column or the "IDmanual" column.}

I want to make a new column in each data frame, based on values from other columns.

I can do this extremely easily for a single data frame using mutate and ifelse;

library(dplyr)

A1stform <- data.frame(a001_idscan = c(123,456,NA), a002_idmanual = c(NA,NA,789))
B2ndform <- data.frame(b001_idscan = c(NA,456,789), b002_idmanual = c(123,NA,NA))
C3rdform <- data.frame(c001_idscan = c(123,456,789), c002_idmanual = c(NA,NA,NA))

A1stform <- A1stform %>%
  mutate(a999_id = ifelse(!is.na(a001_idscan),a001_idscan,a002_idmanual)

But I get in all kinds of trouble when I try to make it into a loop or a function to automatically go through all the dataframes.

I think part the issue is that I can create character strings of the dataframe names and variable names, but I can’t seem to make them refer to the data frame or variable as they just go into the formula as a string.

I’ve read all the documentation about programming in dplyr at vignette("programming", "dplyr”) and tried lots of iterations using quoenquo!! and :=, but I can't make them work.

I’ve also read stack overflow questions and responses here; Use dynamic variable names in `dplyr` and Rename multiple columns by names

But I still can't get it to work.

I did think about trying to come up with a solution using “assign” (I used a loop with “assign” to read all the csvs into the project into the first place) but everytime someone on stack overflow or elsewhere suggested using “assign” someone else comes along and says not to do that, so I haven’t tried very hard to make assign work.

I'm very new to programming, having accidentally stumbled here from an statistics / epidemiology background so any thoughts or advice very much appreciated.


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>