我只需要显示一个列表或数据框,就像在R型控制台中的标记乳胶文档一样,但我真的很难过。我尝试了不同的解决方案但没有取得任何成功。
更具体地说,我有一个包含两列的数据框,我想根据ifelse函数中定义的条件打印一列。
数据结构:
A tibble: 5 x 2
`open_pre_What do you think are the positive points o… `open_pre_What do you think are the negative points of this cour…
<chr> <chr>
1 Very sympathetic and good instructors, very motivated. It was too short and therefore could not unfold it's entirely po…
2 The instructor and TA both were really enthusiastic t… The contents were really compact and intensive - sometimes too m…
3 Provided interesting tools. Since the course provided interesting and useful tools, the time…
4 Very interactive and inspiring instructor and accommo… The time is too limited so that we had to be in a rush to cover …
5 I learnt a lot and got confident that I can do comput… The pace of the exercises on the PC was too fast and hard to fol…
可重复数据
structure(list(`open_pre_What do you think are the positive points of this course?` = c("Very sympathetic and good instructors, very motivated.",
"The instructor and TA both were really enthusiastic to accomplish the goal of the module without making any stragglers.",
"Provided interesting tools.", "Very interactive and inspiring instructor and accommodating TA",
"I learnt a lot and got confident that I can do computer simulations and modelling."
), `open_pre_What do you think are the negative points of this course, and if applicable, what improvements would you suggest?` = c("It was too short and therefore could not unfold it's entirely potential.",
"The contents were really compact and intensive - sometimes too much. This course is undoubtedly recommended but it should have more time.",
"Since the course provided interesting and useful tools, the time was too short to further dig into the details. I.e. a longer course might be helpful.",
"The time is too limited so that we had to be in a rush to cover everything. More time is a must for next",
"The pace of the exercises on the PC was too fast and hard to follow. Timing of the classes could also have been better."
)), .Names = c("open_pre_What do you think are the positive points of this course?",
"open_pre_What do you think are the negative points of this course, and if applicable, what improvements would you suggest?"
), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))
ifelse函数返回字符串&#34;没有注释&#34;如果列表为空,否则返回数据框的第一列
positive_comments = ifelse(identical(my_data[[1]], character(0))==TRUE,"No Comments",(my_data[1]))
print(positive_comments[[1]])
输出:
[1] Very sympathetic and good instructors, very motivated.
[2] The instructor and TA both were really enthusiastic to accomplish the goal of the module without making any stragglers. [3] Provided interesting tools.
[4] Very interactive and inspiring instructor and accommodating TA
[5] I learnt a lot and got confident that I can do computer simulations and modelling.
据我所知,这是因为print()命令插入行仅在需要时断开,并且由于某些原因,在这种情况下,不是。请注意,直接从控制台运行命令会返回所需的结果
然后我尝试转换数据框中的列表,但结果是一样的。
positive_comments = ifelse(identical(my_data[[1]], character(0))==TRUE,"No Comments",(my_data[1]))
positive_comments = as.data.frame(positive)
colnames(positive_comments) <- ""
print(positive_comments)
print(positive_comments)
1非常有同情心和良好的导师,非常积极主动。 2教练和TA都非常热衷于完成模块的目标而不会造成任何落后者。 3提供了有趣的工具。 4非常互动和鼓舞人心的讲师和适应TA 5我学到了很多东西,并且相信我可以进行计算机模拟和建模。
print(positive_comments[1])
1非常有同情心和良好的导师,非常积极主动。 2教练和TA都非常热衷于完成模块的目标而不会造成任何落后者。 3提供了有趣的工具。 4非常互动和鼓舞人心的讲师和适应TA 5我学到了很多东西,并且相信我可以进行计算机模拟和建模。
print(positive_comments[[1]])
[1]非常有同情心和良好的导师,非常积极主动。 [2]教练和TA都非常热衷于完成模块的目标而不会造成任何落后者。 [3]提供了有趣的工具。 [4]非常互动和鼓舞人心的教练和容纳TA [5]我学到了很多东西,并确信我可以进行计算机模拟和建模。
最后,我尝试删除降价代码块中的results=“asis”
选项,但它仍然没有返回所需的结果。在这种情况下,允许打印命令越过降价边界,导致输出不可读。使用print.data.frame
(或其他打印方法)不解决问题。
期望的输出:
[1]非常有同情心和良好的导师,非常积极主动。
[2]教练和TA都非常热衷于完成模块的目标而不会造成任何落后者。
[3]提供了有趣的工具。
[4]非常互动和鼓舞人心的教练和适应性的TA
[5]我学到了很多东西,并且相信我可以进行计算机模拟和建模。