Reputation: 369
I am trying to replace string in multiple dataframes saved in a list with another string. Below is a representative code for what I need. In the example, I am trying to replace the word "white" with "blue" in dat1 and dat2, but the replacement is not working. Is the issue with my indexing of df.list? I tried indexing the df.list$D in as.character(), but an error pops up.
dat1 <- data.frame(c(1,2,3,4), c(10, 20, 30, 40),
c(100, 200, 300, 400), c("red G white 40 R3", "red G white 40 R5",
"red H white 40 R7", "red H white 40 R10"))
dat2 <- data.frame(c("red G white 40 R3", "red G white 40 R5",
"red H white 40 R7", "red H white 40 R10"))
colnames(dat1) <- c("A", "B", "C", "D")
colnames(dat2) <- c("D")
df.list <- list(dat1, dat2)
for(i in 1:length(df.list)){
df.list[[i]]$D <- lapply(df.list, function(df.list) sub("white", "blue",
as.character(df.list$D), fixed = TRUE))
}
Upvotes: 0
Views: 366
Reputation: 887901
We can use transform
in base R
df.list <- lapply(df.list, transform, D = sub('white', 'blue', D))
Or using tidyverse
library(dplyr)
library(purrr)
library(stringr)
df.list <- map(df.list, ~ .x %>%
mutate(D = str_replace(D, 'white', 'blue')))
Upvotes: 0
Reputation: 389275
Don't use for
loop and lapply
. Use either of them :
df.list <- lapply(df.list, function(x) {x$D <- sub('white', 'blue', x$D);x})
df.list
#[[1]]
# A B C D
#1 1 10 100 red G blue 40 R3
#2 2 20 200 red G blue 40 R5
#3 3 30 300 red H blue 40 R7
#4 4 40 400 red H blue 40 R10
#[[2]]
# D
#1 red G blue 40 R3
#2 red G blue 40 R5
#3 red H blue 40 R7
#4 red H blue 40 R10
Use gsub
if you want to replace multiple occurrences of 'white'
with 'blue'
.
To get data back to individual dataframes, name the list and use list2env
.
names(df.list) <- paste0('dat', 1:2)
list2env(df.list, .GlobalEnv)
Upvotes: 1