Sapply/ Lapply Homework

Hello, I would have 2 questions...

n <- 6
sapply(lapply (1:n, ":", 1)sum)

output: [1] 1 3 6 10 15 21

how can I write it using the "for" loop?

Second: There is an output in the picture below... how should the code look like?


Congrats on your first post.

you have miss typo.

I suggest you use markdown for your questions.
pleas Check out the instructions for newbie submissions for more information.

The first thing you should do is to check what each function is doing.

n <- 6

lapply(1:n, ":", 1)

Let's make it possible to output the result every time i changes once.

result <- rep(0,6)

for(i in 1:6){
  data <- 1:i
  res <- sum(data)
  result[i] <- res


For the second question, I don't know the format of the data.
Please create and submit the sample data by yourself.
and show me the code.

Hi Rsky,

thank you for you advice.

My code was this

pizzen <- list(
Margherita = c("Tomaten", "Kaese"),
Cardinale = c("Tomaten", "Kaese", "Schinken"),
"San Romio" = c("Tomaten", "Kaese","Schinken", "Salami", "Mais"),
Provinciale = c("Tomaten", "Kaese","Schinken", "Mais", "Pfefferoni")

but it does not mark every row with numbers 1,2,3,4... so I am not sure about the format

I have removed the image from your post as it violates this forums homework policy.

Homework inspired questions are welcome but they should not include verbatim instructions from your course.

I am rooting for you to complete your code.

I tried itwith this code...but the rows in output still have [1], not [1], [2], [3], [4]

And it should be without using a "for" funktion

for (i in 1:length(pizzen)) {
text = names(pizzen)[i]
ingredients = NULL
for (j in 1:length(pizzen[[i]])) {
ingredients = paste(ingredients, pizzen[[i]][j], " , ", sep="")
print(paste(text, ingredients, sep=": "))

result <- rep(0,6)

for(i in 1:6){
  data <- 1:i
  res <- sum(data)