R中邮政编码的大写

R中邮政编码的大写,r,R,我试图在数据帧中大写一个大的加拿大邮政编码向量。我知道怎么做,但我不知道如何为它编写for循环。加拿大邮政编码采用字母数字格式“A1A 1A1” 假设我的数据如下所示: df <- as.data.frame(matrix(NA, nrow = 10, ncol = 1)) df$V1 <- c('a1a 1a1','b1b 1b1','c1c 1c1','d1d 1d1','e1e 1e1','f1f 1f1','g1g 1g1','h1h 1h1','i1i 1i1','j1j

我试图在数据帧中大写一个大的加拿大邮政编码向量。我知道怎么做,但我不知道如何为它编写for循环。加拿大邮政编码采用字母数字格式“A1A 1A1”

假设我的数据如下所示:

df <- as.data.frame(matrix(NA, nrow = 10, ncol = 1))
df$V1 <- c('a1a 1a1','b1b 1b1','c1c 1c1','d1d 1d1','e1e 1e1','f1f 1f1','g1g 
1g1','h1h 1h1','i1i 1i1','j1j 1j1')
colnames(df) <- 'PostalCodes'

df
toupper
是矢量化的。只需这样做:
toupper(df$PostalCodes)
。toupper忽略数字function@DanY,谢谢你提供的信息,我肯定想得太多了。。。。
x <- c('a1a 1a1')
s <- strsplit(x," ")[[1]]
output <- paste0(toupper(substr(s,1,1)), toupper(substr(s,2,2)), 
toupper(substr(s,3,3)),  collapse = " ")