我想告诉你一个让我忙碌几个小时的案例: 在尝试迭代列表以从中提取最后一个字符时,我需要最后一个字母,它标识了T恤的大小:
Lineitem.name
1 T-Shirt Donna "Si dai. Ciao." - M
2 T-Shirt Donna "Honey" - L
3 T-Shirt Donna "Si dai. Ciao." - M
4 T-Shirt Donna "I do very bad things" - M
5 T-Shirt Donna "Si dai. Ciao." - M
6 T-Shirt Donna "Stai nel tuo (mind your business)" - White / S
7 T-Shirt Donna "Stay Stronz" - White / L
8 T-Shirt Donna "Stay Stronz" - White / M
9 T-Shirt Donna "Si dai. Ciao." - S
10 T-Shirt Donna "Je suis esaurit" - Black / S
到目前为止,我试过这样:
tshirt_sizes <- orders[18] #18 because its the 18th column in my df
subst = function(x,n){
substring(x,nchar(x)-n+1)
}
#looping through the list
typeof(tshirt_sizes)
for(i in tshirt_sizes[]) {
tshirt_sizes[2] <- subst(i, 1)
}
#Using this for loop i get this error:
Error in nchar(x) : 'nchar()' requires a character vector
#second attempt using apply()
apply(tshirt_sizes,1, subst)
#Using this i get this error:
Error in substring(x, nchar(x) - n + 1) :
argument "n" is missing, with no default
答案 0 :(得分:3)
这可以通过正则表达式的魔力在一行中完成:
gsub('.*(.)$', '\\1', orders[[18]])
请注意使用双括号(orders[[18]]
),因为gsub
需要向量,而不是列表。正则表达式是说“返回在字符串结尾之前出现的单个字符”。
由于您对每个字符串的最后一个字符特别感兴趣,您还可以避免使用正则表达式并按以下方式执行:
split <- strsplit(orders[[18]], '')
reversed <- lapply(split, rev)
last_char <- lapply(reversed, '[', 1)