Home > Blockchain >  R - How do I make all values within a list of vectors that are less than zero equal to 0?
R - How do I make all values within a list of vectors that are less than zero equal to 0?

Time:01-24

The set up code. If it runs fine you should get a list of 5 vectors, with each vector having 800 numbers.

h5=rep(0,800);h6=h5;h7=h5;h8=h5;h9=h5
list_knot<- list(h5,h6,h7,h8,h9)
list_length <- length(list_knot) # 5 
x=seq(from=-400,to=400,length.out=800)

vec1 <- list(c(1, 2, 3, 4, 5)) # as per your requirement
list_knot <- lapply(vec1[[1]], function(v, x) x - v^3, x = 1:800)

I was wondering how I could make all of the values within list_knot that are < 0 = 0 within that function or another function?

CodePudding user response:

Another possible solution, using purrr::map:

library(tidyverse)

list_knot <- lapply(vec1[[1]], function(v, x) x - v^3, x = 1:800)

list_knot %>% 
  map(~ if_else(.x <= 0, 0, .x))

CodePudding user response:

I think pmax(0, .), should work, i.e.

list_knot <- lapply(vec1[[1]], function(v, x) pmax(0, x - v^3), x = 1:800)
  •  Tags:  
  • Related