0

I have a list of dataframes and am currently using the following for loop:

  for (i in 1:length(genotypeGOI)){
    genotypeGOI[[i]]$SEQSTRAND <- '*'
  }

But I'd really like to learn how to use lapply properly.

I've tried many different options using the mutate function but nothing is giving me what I want. My latest attempt is:

genotypeGOI <- lapply(X = genotypeGOI, FUN = function(x){
  x <- x$SEQSTRAND, '*')
})

But this is giving me an error:

Error: unexpected ',' in:
"genotypeGOI <- lapply(X = genotypeGOI, FUN = function(x){
  x <- x$SEQSTRAND,"

Basically I would like to know how to change the values in a specific column for each dataframe in a list using lapply and don't really care about this specific problem.

I've looked at the other posted questions related to this and the most similar one says to make a function and to call that in lapply but I really don't want to do that for a one-liner.

Thanks

  • What error does it give you? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 11 '23 at 14:46
  • Or I guess the problem is you're not assigning anything to a column in your `mutate`. Instead of `x <- x %>% mutate_at(vars(SEQSTRAND), '*')`, do `x %>% mutate(SEQSTRAND='*')` – MrFlick Jan 11 '23 at 14:47
  • I'm not looking for this specific example. I'm just trying to see how to convert the for loop into lapply. I can't figure out how to modify a specific column in each dataframe in a list with lapply. I don't seem to be able to use x$ syntax. – WantaghMomma Jan 11 '23 at 14:49
  • Your example doesn't use `x$` at all so it's not clear what the problem would be. Using `x$SEQSTRAND <- '*'; x` would probably also work (again, hard to tell without a reproducible example). You just need to make sure to have `x` at the end so the function returns the entire data.frame. – MrFlick Jan 11 '23 at 14:51
  • When I use: genotypeGOI <- lapply(X = genotypeGOI, FUN = function(x){ x <- x$SEQSTRAND, '*') }) I get the error: Error: unexpected ',' in: "genotypeGOI <- lapply(X = genotypeGOI, FUN = function(x){ x <- x$SEQSTRAND," I've added this to the question as well – WantaghMomma Jan 11 '23 at 14:59
  • That's not what I suggested. I suggested `genotypeGOI <- lapply(X = genotypeGOI, FUN = function(x){ x$SEQSTRAND <- '*'; x })` – MrFlick Jan 11 '23 at 15:02

2 Answers2

1

Your command lapply(genotypeGOI, FUN = function(x) x <- x$SEQSTRAND,'*') doesn't quite make sense in R coding. Even still, if you just did lapply(genotypeGOI, FUN = function(x) x[,"SEQSTRAND"] <- '*') is closer but will still not return what you want:

df <- data.frame(ID = 1:10,
                 X = letters[1:10],
                 SEQSTRAND = NA)

lapply(genotypeGOI, FUN = function(x) x[,"SEQSTRAND"] <- '*')

genotypeGOI <- list(df, df, df)

#[[1]]
#[1] "*"

#[[2]]
#[1] "*"

#[[3]]
#[1] "*" 

To return the data frame with SEQSTRAND as a *, you can use lapply like this (returning the x value)

lapply(genotypeGOI, function(x) {
  x[,"SEQSTRAND"] <- "*"
  x
})

# [[1]]
#      ID X SEQSTRAND
#   1   1 a         *
#   2   2 b         *
#   3   3 c         *
#   4   4 d         *
#   5   5 e         *
#   6   6 f         *
#   7   7 g         *
#   8   8 h         *
#   9   9 i         *
#   10 10 j         *
#   
#   [[2]]
#      ID X SEQSTRAND
#   1   1 a         *
#   2   2 b         *
#   3   3 c         *
#   4   4 d         *
#   5   5 e         *
#   6   6 f         *
#   7   7 g         *
#   8   8 h         *
#   9   9 i         *
#   10 10 j         *
#   
#   [[3]]
#      ID X SEQSTRAND
#   1   1 a         *
#   2   2 b         *
#   3   3 c         *
#   4   4 d         *
#   5   5 e         *
#   6   6 f         *
#   7   7 g         *
#   8   8 h         *
#   9   9 i         *
#   10 10 j         *
jpsmith
  • 11,023
  • 5
  • 15
  • 36
0

If you'd like to stick with mutate as had appeared in the original question, this answer'll work:

dd<-data.frame(Value=c(1,5,7,9))
ee<-data.frame(Value=c(2,4,6,8))

ff<-list(dd,ee)

gg<-lapply(ff, FUN=function(x){
  x%>%
    mutate(SEQSTRAND ="*")
})

We start by making a list of dataframes with ff<-list(dd,ee). When using the pipe (%>%) in the lapply, since we've specified our x to be the list named "ff", we write our function to be applied as a reference to x.

Pake
  • 968
  • 9
  • 24