Anna
Anna

Reputation: 75

Nested lists and function to some elements

I have a nested list data

With str(data) I have the following output

List of 2
$ group_info                  :List of 2
  ..$ lat       : num [1:22] 50.5 55 ...
  ..$ names     : chr [1:22] "A" "B" 
$ param                   : num [1:60, 1:56] 0.0923 0.0952 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:60] "RESULTS" "RESULTS1" "RESULTS2" "RESULTS3" ...
  .. ..$ : chr [1:56] "exp" "pops" ...

I would like to check the values in "pops" column and if them are <0.125, then give then value of 0.125.

How would it be possible to do?

Thanks!

Upvotes: 0

Views: 34

Answers (1)

akrun
akrun

Reputation: 887128

The easiest option is to extract the list element and assign back with pmax of the column and 0.125, thus if any element is less than 0.125, it will still return 0.125

data$param[, "pops"] <- pmax(data$param[, "pops"], 0.125, na.rm = TRUE)

Or if we want to use the <, either ifelse or replace would work

data$param[, "pops"] <- replace(data$param[, "pops"], 
                                data$param[, "pops"] < 0.125, 0.125)

Upvotes: 1

Related Questions