R通过将名称与df列值匹配来填充向量

R通过将名称与df列值匹配来填充向量,r,R,我有一个用零填充的命名向量 toy1<- rep(0, length(37:45)) names(toy1) <- 37:45 我需要帮助找到一种方法,将大小值与向量名称匹配,然后将相应的计数值输入到该向量位置首先,让我们加载数据 toy1<- rep(0, length(37:45)) names(toy1) <- 37:45 df = read.table(text="37 1.181 38 0.421 39 0.054 40

我有一个用零填充的命名向量

toy1<- rep(0, length(37:45))
names(toy1) <- 37:45

我需要帮助找到一种方法,将大小值与向量名称匹配,然后将相应的计数值输入到该向量位置

首先,让我们加载数据

toy1<- rep(0, length(37:45))
names(toy1) <- 37:45
df = read.table(text="37      1.181
38      0.421
39      0.054
40      0.005
41      0.031
42      0.582
45      0.024")
names(df) = c("size","count")
但是,这不是很优雅。相反,您可以将
toy1
转换为data.frame

toydf = data.frame(toy1 = toy1,name = names(toy1),stringsAsFactors = FALSE)
现在,我们可以使用
merge
来获取值

updated = merge(toydf,df,by.x = "name",by.y="size",all.x=T)
这将返回一个3列data.frame。然后,您可以从中提取
count
列,将NA替换为0,就完成了

updated$count[is.na(updated$count)] = 0
updated$count
#> [1] 1.181 0.421 0.054 0.005 0.031 0.582 0.000 0.000 0.024

假设您的数据帧是df,那么您只需更新toy1中的记录即可获得数据帧中的可用记录:

toy1[as.character(df$size)]    <- df$count
toy1[as.character(df$size)]可能非常简单:

toy1[ as.character(dat$size) ] <- dat$count
toy1

#   37    38    39    40    41    42    43    44    45 
#1.181 0.421 0.054 0.005 0.031 0.582 0.000 0.000 0.024 
这是因为出现了数字索引,并且向量的长度有默认的扩展,以容纳最多45个数字

对于数据帧的一个版本,其数字不在37:45范围内,我确实收到了使用nomatch为0的
match
时发出的警告,但我也得到了预期的结果:

toy1[ match( as.character( dat$size), names(toy1) , nomatch=0) ] <- dat$count
#------------
Warning message:
In toy1[match(as.character(dat$size), names(toy1), nomatch = 0)] <- dat$count :
  number of items to replace is not a multiple of replacement length
> toy1
   37    38    39    40    41    42    43    44    45 
1.181 0.421 0.054 0.005 0.031 0.582 0.000 0.000 0.000 

toy1[match(如.character(dat$size)、names(toy1)、nomatch=0)]
count
是数据框架中的一个数字列。这是有效的!感谢您花时间解释为什么
as.character
是必需的一个注意事项-如果
df$size
的值不在
toy1
中,则
toy1
将添加新元素。这不仅仅是更新
toy1
Yes。我一开始是用
match
编码的,如果OP需要这个功能,我可能还会继续使用它。这实际上是一个问题。我只想为
toy1
中命名的大小范围输入数据,而不添加额外的大小如果
df$size
中有一个值不在
toy1
中,那么这将向
toy1
标记添加新元素。联接将是处理它的有效方法。我在为这个案例更新“toy1”之前添加了一个匹配项。我会确保在“size”值未排序时测试最后一个匹配项。OP没有给出一个很好的测试示例。它遗漏了几个边缘案例。@42-我同意这不是一个好例子。我测试过,排序是由匹配“m”负责的,该匹配用于在之后创建逻辑向量。我担心
!is.na(.)
将删除上一次分配左侧的订购信息。。。但我错了。
m <- match(names(toy1), as.character(df$size))
toy1[which(!is.na(m))]    <- df$count[m[!is.na(m)]]
toy1[ as.character(dat$size) ] <- dat$count
toy1

#   37    38    39    40    41    42    43    44    45 
#1.181 0.421 0.054 0.005 0.031 0.582 0.000 0.000 0.024 
toy1[ dat$size ] <- dat$count
> toy1
   37    38    39    40    41    42    43    44    45                                                             
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA 

   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA 1.181 0.421 

0.054 0.005 0.031 0.582    NA    NA 0.024 
toy1[ match( as.character( dat$size), names(toy1) , nomatch=0) ] <- dat$count
#------------
Warning message:
In toy1[match(as.character(dat$size), names(toy1), nomatch = 0)] <- dat$count :
  number of items to replace is not a multiple of replacement length
> toy1
   37    38    39    40    41    42    43    44    45 
1.181 0.421 0.054 0.005 0.031 0.582 0.000 0.000 0.000