Reputation: 371
I have several dataframes within a list so that I can apply functions to each dataframe at once; below is some mock data
df1 <- data.frame(spp = c("Rubsp", "Rusp", "Cest", "Ceor", "Dagl"),
julian = c(60, 70, 80, 90, 100))
df2 <- data.frame(spp = c("Sosp", "Spsp", "Cest", "Umsp", "Dagl"),
julian = c(60, 70, 80, 90, 100))
df3 <- data.frame(spp = c("Syal", "Syla", "Cest", "Umsp", "Ceor"),
julian = c(60, 70, 80, 90, 100))
spp_list <- list(df1=df1, df2=df2, df3=df3)
I am trying to make a function to change some misspellings of certain species names within the dataframes. For example, "Rubsp" needs to be "Rusp". Below is my attempt at this function.
change_spp <- function(df){
(df[["spp"]] == "Rubsp") -> "Rusp"
(df[["spp"]] == "Sosp") -> "Spsp"
(df[["spp"]] == "Syal") -> "Syla"
return(df)
}
spp_list <- lapply(spp_list, change_spp)
When I apply this function to my list of dataframes, nothing changes. I can't figure out the right syntax to get these names to be fixed.
Using R version 4.0.2, Mac OS X 10.13.6
Upvotes: 0
Views: 40
Reputation: 8880
use tidyverse
and replacement table
library(tidyverse)
# replacement table
rplc <- data.frame(spp = c("Rubsp", "Sosp", "Syal"), spp_correct = c("Rusp", "Spsp", "Syla"))
map(spp_list, ~ left_join(.x, rplc) %>%
transmute(spp = coalesce(spp_correct, spp), julian))
#> Joining, by = "spp"
#> Joining, by = "spp"
#> Joining, by = "spp"
#> $df1
#> spp julian
#> 1 Rusp 60
#> 2 Rusp 70
#> 3 Cest 80
#> 4 Ceor 90
#> 5 Dagl 100
#>
#> $df2
#> spp julian
#> 1 Spsp 60
#> 2 Spsp 70
#> 3 Cest 80
#> 4 Umsp 90
#> 5 Dagl 100
#>
#> $df3
#> spp julian
#> 1 Syla 60
#> 2 Syla 70
#> 3 Cest 80
#> 4 Umsp 90
#> 5 Ceor 100
Created on 2020-12-07 by the reprex package (v0.3.0)
Upvotes: 0
Reputation: 39595
Try this:
#Function
change_spp <- function(df){
df$spp[df$spp== "Rubsp"] <- "Rusp"
df$spp[df$spp== "Sosp"] <- "Spsp"
df$spp[df$spp== "Syal"] <- "Syla"
return(df)
}
#Apply
spp_list <- lapply(spp_list, change_spp)
Output:
spp_list
$df1
spp julian
1 Rusp 60
2 Rusp 70
3 Cest 80
4 Ceor 90
5 Dagl 100
$df2
spp julian
1 Spsp 60
2 Spsp 70
3 Cest 80
4 Umsp 90
5 Dagl 100
$df3
spp julian
1 Syla 60
2 Syla 70
3 Cest 80
4 Umsp 90
5 Ceor 100
Your approach would need something like this in order to work (indexing):
#Function
change_spp <- function(df){
df$spp[df[["spp"]] == "Rubsp"] <- "Rusp"
df$spp[df[["spp"]] == "Sosp"] <- "Spsp"
df$spp[df[["spp"]] == "Syal"] <- "Syla"
return(df)
}
#Apply
spp_list2 <- lapply(spp_list, change_spp)
Same output.
Upvotes: 1