Search code examples
rfor-loopapplysapply

Using sapply to list all parts of a vector, but not show the list on the bottom


I have a bit of code, lets say test<-c("A", "B", "C") that I want to list individually line by line. I use sapply(test, FUN=print) and I get

[1] "A"
[1] "B"
[1] "C"
  A   B   C
"A" "B" "C"

but I want to get this instead. Is there any way to do so using any of the apply functions?

[1] "A"
[1] "B"
[1] "C"

I have been able to do this using a for loop

for (i in 1:length(test)){
      print(test[i])
}

but I'm trying to do this with the apply family specifically.


Solution

  • We can use invisible to wrap to that it won't print the output from sapply

    invisible(sapply(test, FUN=print))
    #[1] "A"
    #[1] "B"
    #[1] "C"