Move a large list of files into a folder, and delete them from the original folder in R

Advertisements I am trying to move a large list of files to a newly created folder, and remove the files from the original folder. I will use some data that comes with base R as an example to hopefully make this easy to reproduce. library(dplyr) DNase %>% write.csv("movethis_1.csv") mtcars %>% write.csv("movethis_2.csv") files_to_move <- list.files(pattern =… Read More Move a large list of files into a folder, and delete them from the original folder in R

Using lapply to replace values in a list from randomly sampled values from another list

Advertisements I am trying to replace values in a list word, on indexes specified by the list positions, by sampling values that exist in a third list called letters. Here’s an example of how my lists look like: word <- c("A","E","C","A","R","O","P") positions <- c(1,5,3,7) letters <- c("A","B","C","D","E","F") One important detail is that the value in… Read More Using lapply to replace values in a list from randomly sampled values from another list

Combining multiple columns into one using a function, but using colnames with ascending numbers in their name R

Advertisements In my dataframe the three responses (yes, maybe, no) to a question are now printed as three separate variables (a binary outcome of each possible response). I want to combine the three binary responses to a question into one variable, showing which response was selected. The following piece of code does this: data$var1 <-… Read More Combining multiple columns into one using a function, but using colnames with ascending numbers in their name R

Retain levels when generating multiple plots from dplyr filtered data

Advertisements Looking to retain levels when generating multiple plots via lapply and filtered data: library(purrr) library(tidyverse) test <- data.frame(`1` = rpois(10,3),`2`=rpois(10,3), `3` = rpois(10,3)) ccf.func <- function(i,j){ dat <- ccf(test[i],test[j],plot=FALSE,type="correlation") ccf.dat <- data.frame(ccf=dat$acf,lag=dat$lag,first=i,second=j) return(ccf.dat) } dat <- map_dfr(1:3, function(i) map_dfr(1:3, function(j) ccf.func(i,j))) lapply(0:2, function(i) ggplot(dat |> filter(lag == i)) + geom_tile(aes(x=first,y=second,fill=ccf))) These all have differing… Read More Retain levels when generating multiple plots from dplyr filtered data