R 使用另一列填充数据框中的某些行

R 使用另一列填充数据框中的某些行,r,regex,dataframe,R,Regex,Dataframe,我正在尝试填充列size,其值为0。新值应该是列name的最后一段。我尝试了许多方法,但当我尝试仅用0填充行时,它不会返回正确的值: 样本数据: vertices=data.frame(name=c("a","b","c","a.b","a.c","a.a.9","a.b.8"),size= c(1,5,6,2,6,0,0)) ID name size 1 a 1 2 b 5 3 c 6 4 a.b 2 5 a.c 6 6 a.a.

我正在尝试填充列
size
,其值为0。新值应该是列
name
的最后一段。我尝试了许多方法,但当我尝试仅用0填充行时,它不会返回正确的值:

样本数据:

vertices=data.frame(name=c("a","b","c","a.b","a.c","a.a.9","a.b.8"),size= c(1,5,6,2,6,0,0))

ID name size
1     a    1
2     b    5
3     c    6
4   a.b    2
5   a.c    6
6 a.a.9    0
7 a.b.8    0
这一行按预期工作,但我不想触及大小不为0的行:

vertices$size <- sub(".*\\.", "", vertices$name)

ID name size
1     a    a
2     b    b
3     c    c
4   a.b    b
5   a.c    c
6 a.a.9    9
7 a.b.8    8

顶点$size看起来您试图保存错误大小的向量。在最后一行中,你可能只是想

vertices$size[vertices$size==0] <- sub(".*\\.", "", vertices$name[vertices$size==0]) 

顶点$size[顶点$size==0]看起来您试图保存的向量大小不正确。在最后一行中,你可能只是想

vertices$size[vertices$size==0] <- sub(".*\\.", "", vertices$name[vertices$size==0]) 

顶点$size[顶点$size==0]我们甚至需要对
名称进行子集

vertices$size[vertices$size==0]<- sub(".*\\.", "", vertices$name[vertices$size==0])

vertices
#   name size
#1     a    1
#2     b    5
#3     c    6
#4   a.b    2
#5   a.c    6
#6 a.a.9    9
#7 a.b.8    8
正如@Frank在comments
size
中提到的,由于正则表达式方法,它的类从数字变为字符

sub(".*\\.", "", vertices$name[vertices$size==0])
#[1] "9" "8"
如果我们需要维护
size
列的类,我们可以使用
as.numeric

as.numeric(sub(".*\\.", "", vertices$name[vertices$size==0]))
#[1] 9 8

然而,这再次假设
size=0
最后一个字符是一个数字,如果它是一个字符,它将返回
NA

我们甚至需要对
名称进行子集

vertices$size[vertices$size==0]<- sub(".*\\.", "", vertices$name[vertices$size==0])

vertices
#   name size
#1     a    1
#2     b    5
#3     c    6
#4   a.b    2
#5   a.c    6
#6 a.a.9    9
#7 a.b.8    8
正如@Frank在comments
size
中提到的,由于正则表达式方法,它的类从数字变为字符

sub(".*\\.", "", vertices$name[vertices$size==0])
#[1] "9" "8"
如果我们需要维护
size
列的类,我们可以使用
as.numeric

as.numeric(sub(".*\\.", "", vertices$name[vertices$size==0]))
#[1] 9 8

然而,这再次假设最后一个字符是数字,如果它是一个字符,它将返回
NA
stringr
dplyr

vertices %>% mutate(size = ifelse(size > 0, size, str_extract(name, "[0-9]+")))

使用
stringr
dplyr

vertices %>% mutate(size = ifelse(size > 0, size, str_extract(name, "[0-9]+")))

我们也可以使用
regmatches/regexpr

vertices$size[!vertices$size] <- 
    as.numeric(regmatches(vertices$name, regexpr("\\d+$", vertices$name)))

vertices
#   name size
#1     a    1
#2     b    5
#3     c    6
#4   a.b    2
#5   a.c    6
#6 a.a.9    9
#7 a.b.8    8

顶点$size[!顶点$size]我们也可以使用
regmatches/regexpr

vertices$size[!vertices$size] <- 
    as.numeric(regmatches(vertices$name, regexpr("\\d+$", vertices$name)))

vertices
#   name size
#1     a    1
#2     b    5
#3     c    6
#4   a.b    2
#5   a.c    6
#6 a.a.9    9
#7 a.b.8    8

vertices$size[!vertices$size]如果您决定将其作为答案发布,那么将其从注释中删除怎么样我猜
size
以数字开头,并强制在此处使用字符串。使用data.table的另一个很好的理由是,它会发出警告,然后将字符强制为数字,而不是反过来…@Frank使用regex做到了这一点。我们总是可以用
as.numeric
来包装它以维护类。我将更新答案。如果您决定将其作为答案发布,那么从注释
size
中删除它如何?我猜,它以数字开头,并强制在此处使用字符串。使用data.table的另一个很好的理由是,它会发出警告,然后将字符强制为数字,而不是反过来…@Frank使用regex做到了这一点。我们总是可以用
as.numeric
来包装它以维护类。我会更新答案。