1

I have a problem applying a function to list elements. I have a list called "mylist", which looks like:

[[1]]  station global
       1        2
       1        2
       1        2
       1       14
       1       38
       1      169

[[2]] station global
       2       2
       2       2
       2      23
       2      86

In each list, I need to set values of "global" less than or equal to 2 to NA. I have used

dat.list <- lapply(mylist, ``[[``, 'global') to get only the global data.

Defining af function:

fct <- function(x) {
x[x <= 2] <- NA
}

and writing lapply(dat.list, fct) gives

[[1]] NA
[[2]] NA

What I would like to have is:

[[1]] station global
      1       NA
      1       NA
      1       NA
      1       14
      1       38
      1      169

[[2]] station global
      2       NA
      2       NA
      2       23
      2       86

I apprechiate any help or a point in the right direction, Regards Sisse

Sisse
  • 291
  • 1
  • 4
  • 14

1 Answers1

1

It would help if you posted a reproducible example. See here for advice on how to do this.

x will take on the element of the list. Since those appear to be data.frames, treat x as a data.frame:

fct <- function(x) {
   x$global[x$global <= 2] <- NA
   x
}
Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235