R 如何删除列中的变量字符?
我试图从数据帧中的列中删除附加的“s.#”:R 如何删除列中的变量字符?,r,R,我试图从数据帧中的列中删除附加的“s.#”: Species <- c("Dogs.1","Dogs.2","Dogs.3","Cats.1","Cats.2","Cats.3") Breed <- c("Great Dane","Beagle","Beagle","Bengal","Tabby","Siamese") names(Species) <- "Species" names(Breed) <- "Breed" pets <- as.data.fram
Species <- c("Dogs.1","Dogs.2","Dogs.3","Cats.1","Cats.2","Cats.3")
Breed <- c("Great Dane","Beagle","Beagle","Bengal","Tabby","Siamese")
names(Species) <- "Species"
names(Breed) <- "Breed"
pets <- as.data.frame(cbind(Species,Breed))
我希望输出更像这样:
Species Breed
1 Dog Great Dane
2 Dog Beagle
3 Dog Beagle
4 Cat Bengal
5 Cat Tabby
6 Cat Siamese
是否有一种方法可以操作“种类”列以删除“#”?编辑:要从“种类”列中删除s
,请使用以下命令
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
要覆盖小型和资本sS
也使用以下内容
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
你能试试下面的吗
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
或者,如果“种类”列中始终有.位
,则使用以下内容
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
如果您想将输出保存在数据框的列中,请使用以下命令
pets$Species <- sub("\\..*","",pets$Species)
pets$Species编辑:要从Species列中删除s
,请使用以下命令
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
要覆盖小型和资本sS
也使用以下内容
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
你能试试下面的吗
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
或者,如果“种类”列中始终有.位
,则使用以下内容
sub("s\\..*","",pets$Species)
sub("[Ss]\\..*","",pets$Species)
sub("\\..*","",pets$Species)
sub("\\.[0-9]+","",pets$Species)
如果您想将输出保存在数据框的列中,请使用以下命令
pets$Species <- sub("\\..*","",pets$Species)
pets$Species我们可以在这里使用sub
。下面的图案将删除一个点,后跟一个或多个数字,这是种类
文本中的最后一个数字。我还删除了一个可选字母s
,它可能(也可能不)出现在点之前
pets$Species <- sub("s?\\.\\d+$", "", pets$Species)
pets
Species Breed
1 Dog Great Dane
2 Dog Beagle
3 Dog Beagle
4 Cat Bengal
5 Cat Tabby
6 Cat Siamese
pets$Species我们可以在这里使用sub
。下面的图案将删除一个点,后跟一个或多个数字,这是种类
文本中的最后一个数字。我还删除了一个可选字母s
,它可能(也可能不)出现在点之前
pets$Species <- sub("s?\\.\\d+$", "", pets$Species)
pets
Species Breed
1 Dog Great Dane
2 Dog Beagle
3 Dog Beagle
4 Cat Bengal
5 Cat Tabby
6 Cat Siamese
pets$Species这里是另一个解决方案:
library(stringr)
str_extract(pets$Species, "^.*(?=s)")
[1] "Dog" "Dog" "Dog" "Cat" "Cat" "Cat"
我经常发现,当一个数据帧是长格式,字符串被格式化为某物或某物时,末尾附加的#可以保存有价值的信息,这些信息可以用于分组、刻面、统计和/或数据可视化。但是,我不确定这是否是您的情况,但这里有一种方法可以将两位信息分开以保留附加的信息
library(tidyr)
library(dplyr)
library(stringr)
new_pets <- pets %>%
separate(col = Species, into = c("type", "owner"), sep = "\\.") %>%
mutate(type = str_extract(type, "^.*(?=s)"))
new_pets
# type owner Breed
# 1 Dog 1 Great Dane
# 2 Dog 2 Beagle
# 3 Dog 3 Beagle
# 4 Cat 1 Bengal
# 5 Cat 2 Tabby
# 6 Cat 3 Siamese
library(tidyr)
图书馆(dplyr)
图书馆(stringr)
新宠物%
分离(col=物种,分为c(“类型”、“所有者”),sep=“\\”)%>%
突变(类型=str_extract(类型,“^.*(=s)”)
新宠物
#型主品种
#1只狗1只大丹狗
#2只狗2只小猎犬
#3只狗3只小猎犬
#4第一类孟加拉
#5第2类虎斑猫
#6第3类暹罗语
这里是另一个解决方案:
library(stringr)
str_extract(pets$Species, "^.*(?=s)")
[1] "Dog" "Dog" "Dog" "Cat" "Cat" "Cat"
我经常发现,当一个数据帧是长格式,字符串被格式化为某物或某物时,末尾附加的#可以保存有价值的信息,这些信息可以用于分组、刻面、统计和/或数据可视化。但是,我不确定这是否是您的情况,但这里有一种方法可以将两位信息分开以保留附加的信息
library(tidyr)
library(dplyr)
library(stringr)
new_pets <- pets %>%
separate(col = Species, into = c("type", "owner"), sep = "\\.") %>%
mutate(type = str_extract(type, "^.*(?=s)"))
new_pets
# type owner Breed
# 1 Dog 1 Great Dane
# 2 Dog 2 Beagle
# 3 Dog 3 Beagle
# 4 Cat 1 Bengal
# 5 Cat 2 Tabby
# 6 Cat 3 Siamese
library(tidyr)
图书馆(dplyr)
图书馆(stringr)
新宠物%
分离(col=物种,分为c(“类型”、“所有者”),sep=“\\”)%>%
突变(类型=str_extract(类型,“^.*(=s)”)
新宠物
#型主品种
#1只狗1只大丹狗
#2只狗2只小猎犬
#3只狗3只小猎犬
#4第一类孟加拉
#5第2类虎斑猫
#6第3类暹罗语
@Devin-Answer已更新。“您没有提到s
,所以我最初没有谈到这一点。”Devin回答已更新。您没有提到s
,因此我最初没有介绍它。您需要数据表的特定内容吗?否则,您可能需要删除该标记是否需要特定于data.table
?否则,您可能想删除该标记。您可以使用sep=“s?\\”
,然后您就不需要mutate
行智能,我没有想到。您可以使用sep=“s?\\”
然后您就不需要mutate
行智能,我没有想到这一点。