Problem
I have a dataset df
(see below) with several columns. And I would like to concatenate all columns and print a single paragraph row by row as you can see in Expected output
. I would like to use \r\n
as the syntax to create a paragraph in the output.
Data to import
library(tibble)
df <- tibble(ID = c(001, 002, 003, 004),
resp = c("this is the response of the subject one... \r\n and more and more...",
"Host: this is the response of the subject two",
"now a response from subejct three",
"that is a different one, \r\n and it is from subject 4"),
comment = c("now a comment from the assessor",
NA,
"No comments",
"I don't agree with this comment"))
Input
# A tibble: 4 × 3
ID resp comment
<dbl> <chr> <chr>
1 1 this is the response of the subject one now a comment from the assessor
2 2 this is the response of the subject two NA
3 3 now a response from subejct three No comments
4 4 that is a different one, and it is from subject 4 I don't agree with this comment
expected output
"
Participant: 001
Response: this is the response of the subject one...
and more and more...
Comment: now a comment from the assessor
Participant: 002
Response: Host: this is the response of the subject two
Comment: NA
Participant: 003
Response: now a response from subejct three
Comment: No comments
Participant: 004
Response: that is a different one,
and it is from subject 4
Comment: I don't agree with this comment "
CodePudding user response:
out_string = ""
for (i in 1:nrow(df)) {
out_string <- sprintf("%sParticipant: d \r\nResponse: %s \r\nComment: %s \r\n\r\n" ,
out_string, i, df$resp[i],df$comment[i])
}
cat(out_string)
CodePudding user response:
Fetching cell wise values and using them.
for (i in 1:nrow(df))
{
for (j in 1:ncol(df))
{
a<-as.character(df[i,j])
b<-as.character(df[i,j])
c<-as.character(df[i,j])
if(j==1){
cat("Participant : ",a)
cat("\n")
}else if(j==2){
cat("Response : ",b)
cat("\n")
}else if(j==3){
cat("Comment : ",c)
cat("\n")
}
}
cat("\n")
}