I am using R markdown to create student feedback sheets, based on survey data we collect from their assessors. Input has 10 questions imported in *.csv format. Example data:
question <- as.factor(c(NA, NA, "response 1.", "response 2.", NA, "response 3."))
At present I am running r levels(question)[-1]
in my *.rmd
file which gives me this output (with ", " as separators):
response 1., response 2., response 3.
Note that there are always some NAs so the [-1] is just to remove the NA level of the factor.
What I want is to create output with each response on a new line:
response 1.
response 2.
response 3.
I can do this on single forms by specifying the levels on each level i.e.
r levels(question)[2]
r levels(question)[3]
r levels(question)[4]
However for each question there are variable numbers of non-NA responses (1 up to 4) so I need a solution which does not explicitly have to know how many there will be. i.e. same code will give desired formatting for both q1 and q2:
q1 <- as.factor(c(NA, NA, "response 1.", NA, NA, "response 2.")
q2 <- as.factor(c(NA, NA, "response 1.", "response 2.", "response 3.", "response 4.")
Usually I would address this with a for-loop but Markdown doesn't read code the same way as R console. Have tried as.character
rather than as.factor
but didn't seem to help; calling levels(question[-1])
seemed most straighforward method to remove NA
s.
Thanks!