I'm rather new to R and I guess there's more than one thing inadequate practice in my code (like, using a for loop). I think in this example, it could be solved better with something from the apply-family, but I would have no idea how to do it in my original problem - so, if possible, please let the for-loop be a for-loop. If something else is bad, I'm happy to hear your opinion.
But my real problem is this. I have:
name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b")
class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3")
value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100)
df <- data.frame(name, class, value)
df
And i want to print it out. I use sink as well as hwriter (to output it as a html) later on. I get the problem with both, so I hope it's caused by the same and it's enough if we solve it for sink. That's the code:
sink("stuff.txt")
for (i in 1:nrow(df)) {
cat(" Name:")
cat(df$name[i-1])
cat("\n")
cat(" Class:")
cat(df$class[i-1])
cat("\n")
}
sink()
file.show("stuff.txt")
Part of the output I get is something like:
Name:1
Class:1
Name:1
Class:2
Name:1
Class:2
On the other hand, the output I want should be like:
Name:a
Class:c1
Name:a
Class:c2
Name:a
Class:c2