假设我有一个数据框df
。R:如何在数据框中用大写字母分隔每个单词的首字母?
> df <- data.frame(Disease = c('Disease Entry1; disease Entry2', 'disease Entry4','disease Entry5; disease entry6'), ID = c(1,2,3))
> df
Disease ID
1 Disease Entry1; disease Entry2 1
2 disease Entry4 2
3 disease Entry5; disease entry6 3
我该如何操作它,以便每个疾病条目都是小写字母,除了每个条目的第一个字母?即
> df
Disease ID
1 Disease entry1; Disease entry2 1
2 Disease entry4 2
3 Disease entry5; Disease entry6 3
我以为我会采取使用tolower
功能的莫名其妙,但如何兼顾分号?
是它总是应该以大写字母开头的单词“疾病”?或者这些单词可以改变吗?如果不是,我建议使用'tolower()',然后使用'gsub(“疾病”,“疾病”,df $疾病)' –
应该更清楚@KenS。,'疾病'只是一个占位符I用于这个例子。它确实在变化。 –
你可以使用'stringr :: str_to_title'来转换所有的单词以大写开头 –