如何使用函数将单列R数据框中的第一个字母转换为大写?
要将 R 中单列数据框中的第一个字母转换为大写,我们可以按照以下步骤操作 -
首先,创建一个带有字符串列的数据框。
然后,使用R.utils包中的大写函数将第一个字母转换为单列大写。
示例
创建数据框
让我们创建一个数据框,如下所示 -
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
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
Names1 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包中的大写函数将名称列中的第一个字母转换为大写 -
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
Names1 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
以上是 如何使用函数将单列R数据框中的第一个字母转换为大写? 的全部内容, 来源链接: utcz.com/z/356117.html