Skip to content

Instantly share code, notes, and snippets.

@tylerlittlefield
Last active November 16, 2018 20:18
Show Gist options
  • Save tylerlittlefield/22f180151f3802788ae16016a97f883a to your computer and use it in GitHub Desktop.
Save tylerlittlefield/22f180151f3802788ae16016a97f883a to your computer and use it in GitHub Desktop.
Replace column values in each list element by column index
x <- list(iris[1:5, ], iris[1:5, ])
replace_list <- function(x, value, col) {
x[[col]] <- value
return(x)
}
lapply(x, replace_list, 7, 1)
#> [[1]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 7 3.5 1.4 0.2 setosa
#> 2 7 3.0 1.4 0.2 setosa
#> 3 7 3.2 1.3 0.2 setosa
#> 4 7 3.1 1.5 0.2 setosa
#> 5 7 3.6 1.4 0.2 setosa
#>
#> [[2]]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 7 3.5 1.4 0.2 setosa
#> 2 7 3.0 1.4 0.2 setosa
#> 3 7 3.2 1.3 0.2 setosa
#> 4 7 3.1 1.5 0.2 setosa
#> 5 7 3.6 1.4 0.2 setosa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment