print无法在ifelse语句后打印具有适当索引号的列表

时间:2018-05-15 10:49:58

标签: r

我有一个包含两列的数据框,我想根据ifelse函数中定义的条件打印一列。有时索引号消失,在同一行上生成两个条目。

数据结构:

A tibble: 5 x 2
  `open_pre_What do you think are the positive points of this course?` `open_pre_What do you think are the negative point…
  <chr>                                                                <chr>                                              
1 good summary, great room for questions,                              the course was not enough structured, it was very …
2 The course was very clear.                                           The teaching in the first day was, maybe, a few sl…
3 *ability to get clear about the basics                               *course structure visible for participants  *more …
4 Very clear                                                           The first day was a little bit slow!               
5 the raised topis were interesting                                    no structure, no agenda, no overview in the begini…

可重复数据

structure(list(`open_pre_What do you think are the positive points of this course?` = c("good summary, great room for questions,", 
"The course was very clear.", "*ability to get clear about the basics", 
"Very clear", "the raised topis were interesting"), `open_pre_What do you think are the negative points of this course, and if applicable, what improvements would you suggest?` = c("the course was not enough structured, it was very easy to get lost,", 
"The teaching in the first day was, maybe, a few slow!", "*course structure visible for participants  *more connection to the advanced stats course", 
"The first day was a little bit slow!", "no structure, no agenda, no overview in the begining about the course content, too many questions to the class, no time to let contents sink in"
)), .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函数返回字符串“no comments”如果列表为空,否则返回数据框的第一列

positive_comments = ifelse(identical(my_data[[1]], character(0))==TRUE,"No Comments",(my_data[1]))
positive_comments = unlist(positive_comments) #just to remove the column index 
print(positive_comments)

输出:

[1] "good summary, great room for questions," "The course was very clear."             
[3] "*ability to get clear about the basics"  "Very clear"                             
[5] "the raised topis were interesting"      

期望的输出:

[1] "good summary, great room for questions," 
[2]"The course was very clear."             
[3] "*ability to get clear about the basics"  
[4]"Very clear"                             
[5] "the raised topis were interesting"   

编辑:

在数据框中转换它似乎无济于事。

positive_comments = as.data.frame(positive_comments)
print(positve_comments)

 c..good.summary..great.room.for.questions.....The.course.was.very.clear....
1                                     good summary, great room for questions,
2                                                  The course was very clear.
3                                      *ability to get clear about the basics
4                                                                  Very clear
5                                           the raised topis were interesting

print(positve_comments[[1]])
[1] good summary, great room for questions, The course was very clear.              *ability to get clear about the basics 
[4] Very clear                              the raised topis were interesting      
5 Levels: *ability to get clear about the basics good summary, great room for questions, ... Very clear

编辑2:

手动删除列名称请勿解决降价问题

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)
1 good summary, great room for questions,
2              The course was very clear.
3  *ability to get clear about the basics
4                              Very clear
5       the raised topis were interesting

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

尝试print.simple.list(positive_comments)