Lydie
June 9, 2021, 6:45am
1
Hello, I would have 2 questions...
First:
Code:
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?
Thanks!
Rsky
June 9, 2021, 6:54am
2
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
}
result
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.
Lydie
June 9, 2021, 9:10am
3
Hi Rsky,
thank you for you advice.
My code was this
Lydie
June 9, 2021, 9:12am
4
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.
Can I ask questions from a course I am taking here?
General questions are always welcome!
Please do ask general questions about things like:
How to use R
How to use the RStudio IDE or RStudio Cloud
How to work with tidyverse packages
Where to find resources to help you learn or solve problems
Specific questions can be OK, if you follow these rules:
Never copy-paste instructions from an assignment (even for online courses).
Explicitly mention the course you are taking and use the #homework tag.
Ask your question as a reproducible example (reprex) , preferably prepared for posting using the reprex package .
Want to maximize your chances of getting the help you need? Keep readin…
Rsky
June 9, 2021, 10:21am
6
I am rooting for you to complete your code.
Lydie
June 16, 2021, 12:13pm
7
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)
print(res)
}