如何在 R 中使用函数将单列数据框的第一字母转换为大写?
要将 R 中单列数据框的第一字母转换为大写,我们可以按照以下步骤操作:
首先,创建一个包含字符串列的数据框。
然后,使用 R.utils 包中的 capitalize 函数将单列中的第一个字母转换为大写。
示例
创建数据框
让我们创建一个如下所示的数据框:
Names<- sample(c("rahul","rosy","hidayah","seema","john","sarbat","shaun","sam","teena","ila","kunal","sudha","anil","yukti","jerry","tom"),25,replace=TRUE) df<-data.frame(Names) df
输出
执行上述脚本后,将生成以下输出(由于随机化,此输出可能因您的系统而异):
Names 1 yukti 2 john 3 anil 4 shaun 5 shaun 6 rahul 7 ila 8 kunal 9 anil 10 jerry 11 hidayah 12 yukti 13 shaun 14 teena 15 tom 16 seema 17 jerry 18 teena 19 jerry 20 hidayah 21 shaun 22 hidayah 23 sam 24 rosy 25 seema
将第一个字母转换为大写
使用 R.utils 包中的 capitalize 函数将 Names 列中的第一个字母转换为大写:
Names<- sample(c("rahul","rosy","hidayah","seema","john","sarbat","shaun","sam","teena","ila"," kunal","sudha","anil","yukti","jerry","tom"),25,replace=TRUE) df<-data.frame(Names) library(R.utils) df$Names<-capitalize(df$Names) df
输出
Names 1 Ila 2 Rosy 3 Ila 4 Kunal 5 Jerry 6 Seema 7 Rosy 8 Sam 9 Ila 10 Sudha 11 Seema 12 Jerry 13 Tom 14 Rosy 15 Shaun 16 John 17 Ila 18 Tom 19 Rosy 20 Hidayah 21 Rosy 22 Sarbat 23 John 24 Sarbat 25 Jerry
广告