Hi @Yarnabrina and All,
I am still learning "for loops".
I try to resolve this very simple task:
x <- c(1:5)
for (i in x)
{ y <- i*i
print(y)
}
using sapply and lappy solution presented in my first question:
sapply(c("sd", "mean"), function(fun)lapply(lapply(lapply(2:20, seq,
from=1), function(.x)x[.x]), fun))
because I somehow like it, when it gives me immediately two columns with results showing each and every iteration steps.
Of course I can use the second solution presented there as well:
ID <- c(1,2,3,4,5)
DF <- cbind(ID, expon2) %>% as.data.frame()
colnames(DF) <- c("ID", "result_of_each_Iterations")
and using reprex:
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 4.0.0
#> Warning: package 'forcats' was built under R version 3.6.3
library(magrittr)
#>
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#>
#> set_names
#> The following object is masked from 'package:tidyr':
#>
#> extract
x <- c(1:5)
expon2 <- list()
for (i in 1:5) {
expon2[i] <- i**2
}
expon2 <- unlist(expon2)
ID <- c(1,2,3,4,5)
DF <- cbind(ID, expon2) %>% as.data.frame()
colnames(DF) <- c("ID", "result_of_each_Iterations")
DF
#> ID result_of_each_Iterations
#> 1 1 1
#> 2 2 4
#> 3 3 9
#> 4 4 16
#> 5 5 25
Created on 2020-03-28 by the reprex package (v0.3.0)
but if you could help me do it with sapply and lapply, please ?
And is it a way to do something in order to DF when displayed is not so clustered (header text) but looks like align center (a bit like in MS Word):

kind regards.