I am very new to R, and I could not find a simple example online of how to remove the last n characters from every element of a vector (array?)
我对R很陌生,我在网上找不到一个简单的例子来说明如何从向量的每个元素(数组?)中删除最后的n个字符。
I come from a Java background, so what I would like to do is to iterate over every element of a$data
and remove the last 3 characters from every element.
我来自Java背景,所以我想做的是遍历$data的每个元素,并从每个元素中删除最后3个字符。
How would you go about it?
你会怎么做?
4 个解决方案
#1
60
Here is an example of what I would do. I hope it's what you're looking for.
这是我要做的一个例子。我希望这就是你要找的。
char_array = c("foo_bar","bar_foo","apple","beer")
a = data.frame("data"=char_array,"data2"=1:4)
a$data = substr(a$data,1,nchar(a$data)-3)
a should now contain:
现在应该包含:
data data2
1 foo_ 1
2 bar_ 2
3 ap 3
4 b 4
#2
54
Here's a way with gsub
:
这里有一个关于gsub的方法:
cs <- c("foo_bar","bar_foo","apple","beer")
gsub('.{3}$', '', cs)
# [1] "foo_" "bar_" "ap" "b"
#3
10
The same may be achieved with the stringi package:
stringi包也可以实现同样的目标:
library('stringi')
char_array <- c("foo_bar","bar_foo","apple","beer")
a <- data.frame("data"=char_array, "data2"=1:4)
(a$data <- stri_sub(a$data, 1, -4)) # from the first to the last but 4th char
## [1] "foo_" "bar_" "ap" "b"
#4
9
Although this is mostly the same with the answer by @nfmcclure, I prefer using stringr
package as it provdies a set of functions whose names are most consistent and descriptive than those in base R (in fact I always google for "how to get the number of characters in R" as I can't remember the name nchar()
).
虽然这是主要由@nfmcclure相同的答案,我更喜欢使用stringr的包,因为它provdies一组函数的名字是最一致的和描述性的比基R(事实上我总是谷歌“如何得到的字符数R”我不记得这个名字nchar())。
library(stringr)
str_sub(iris$Species, 1, str_length(iris$Species)-3)
This removes the last 3 characters from each value at Species
column.
这将从Species列的每个值中删除最后3个字符。
#1
60
Here is an example of what I would do. I hope it's what you're looking for.
这是我要做的一个例子。我希望这就是你要找的。
char_array = c("foo_bar","bar_foo","apple","beer")
a = data.frame("data"=char_array,"data2"=1:4)
a$data = substr(a$data,1,nchar(a$data)-3)
a should now contain:
现在应该包含:
data data2
1 foo_ 1
2 bar_ 2
3 ap 3
4 b 4
#2
54
Here's a way with gsub
:
这里有一个关于gsub的方法:
cs <- c("foo_bar","bar_foo","apple","beer")
gsub('.{3}$', '', cs)
# [1] "foo_" "bar_" "ap" "b"
#3
10
The same may be achieved with the stringi package:
stringi包也可以实现同样的目标:
library('stringi')
char_array <- c("foo_bar","bar_foo","apple","beer")
a <- data.frame("data"=char_array, "data2"=1:4)
(a$data <- stri_sub(a$data, 1, -4)) # from the first to the last but 4th char
## [1] "foo_" "bar_" "ap" "b"
#4
9
Although this is mostly the same with the answer by @nfmcclure, I prefer using stringr
package as it provdies a set of functions whose names are most consistent and descriptive than those in base R (in fact I always google for "how to get the number of characters in R" as I can't remember the name nchar()
).
虽然这是主要由@nfmcclure相同的答案,我更喜欢使用stringr的包,因为它provdies一组函数的名字是最一致的和描述性的比基R(事实上我总是谷歌“如何得到的字符数R”我不记得这个名字nchar())。
library(stringr)
str_sub(iris$Species, 1, str_length(iris$Species)-3)
This removes the last 3 characters from each value at Species
column.
这将从Species列的每个值中删除最后3个字符。