简体   繁体   English

比较R中不同长度的两个向量

[英]Compare two vectors of different length in R

I've been struggling with this problem, and decided to ask for some help after some fails.. 我一直在努力解决这个问题,并决定在一些失败之后寻求一些帮助。

Here is my problem, I want to divide these two vectors based on the day, for instance 2012-12-11 will be 3/17 and 2012-12-12 should be 0/7. 这是我的问题,我想根据当天划分这两个向量,例如2012-12-11将是3/17而2012-12-12应该是0/7。 However I can't seem to figure out how to do this.. 但是我似乎无法弄清楚如何做到这一点..

> ili

2012-12-11 2012-12-13 2012-12-14 2012-12-17 
     3          6          7          1 
> no.ili

2012-12-11 2012-12-12 2012-12-13 2012-12-14 2012-12-15 2012-12-16 2012-12-17 
    17          7        232        322         38         21         36 

The last attempt was to loop over the two vectors and add the value or zero to a new vector however when I use %in% it doesn't put the values in order (obviously) but if I use == it also doesn't work.. 最后一次尝试是循环遍历两个向量并将值或零添加到新向量但是当我使用%in%它不会按顺序放置值(显然)但是如果我使用==它也不会工作..

 days.ili <- unique(one.three$timestamp)
 days <- unique(one.week$timestamp)
 ili.vec <- rep(0, length(days))

 for (i in 1:length(days)) {
     if (days.ili[i] %in% days) {
         ili.vec[i] <- ili[i]
     } else {
         ili.vec[i] <- 0
     }
 }

I must be forgetting some thing since I'm not being able to see through this problem.. Can anyone give me any idea about the best way to achieve this in R? 我必须忘记一些事情,因为我无法看清这个问题。任何人都可以告诉我在R中实现这个目标的最佳方法吗?

Perhaps an option will be using merge .. 也许一个选项将使用merge ..

Something like this: 像这样的东西:

res <- rep(0, length(no.ili))
where <- match( names(ili), names(no.ili) )
res[ where ] <- ili / no.ili[where]
names(res) <- names(no.ili)
res
# 2012-12-11 2012-12-12 2012-12-13 2012-12-14 2012-12-15 2012-12-16 2012-12-17 
# 0.17647059 0.00000000 0.02586207 0.02173913 0.00000000 0.00000000 0.02777778

Romain's solution is much cleaner, but assumes no.ili will always be longer.. Romain的解决方案更清洁,但假设no.ili永远更长。

ili <- 
    c( 3 , 6 , 7 , 1 )
names( ili ) <- 
    as.Date( c( '2012-12-11' , '2012-12-13' , '2012-12-14' , '2012-12-17' ) )

no.ili <- 
    c( 17 , 7 , 232 , 322 , 38 , 21 , 36 )
names( no.ili ) <- 
    as.Date( c( '2012-12-11' , '2012-12-12' , '2012-12-13' , '2012-12-14' , '2012-12-15' , '2012-12-16' , '2012-12-17' ) )


ili.df <- data.frame( ili )
ili.df$Date <- rownames( ili.df )

no.ili.df <- data.frame( no.ili )
no.ili.df$Date <- rownames( no.ili.df )

x <- merge( ili.df , no.ili.df , all = TRUE )

x[ is.na( x ) ] <- 0

result <- x$ili / x$no.ili

names( result ) <- x$Date

result

Yes, merge is one possible solution. 是的,合并是一种可能的解决方案。 The trick is, to restate the ili/no.ili data frames with their column names as an additional variable, aka long format. 诀窍是,重新显示ili / no.ili数据框及其列名作为附加变量,即长格式。 Then use merge with the all argument being set to TRUE: 然后使用merge并将all参数设置为TRUE:

ili2 <- data.frame(date=colnames(ili),                                                                                                                
                   ili=as.numeric(ili[1,]),                                                                                                           
                   stringsAsFactors=FALSE)                                                                                                            
no.ili2 <- data.frame(date=colnames(no.ili),                                                                                                          
                      no.ili=as.numeric(no.ili[1,]),                                                                                                  
                      stringsAsFactors=FALSE)                                                                                                         

tmp <- merge(ili2, no.ili2, all=TRUE)                                                                                                      

Then to execute the division you asked for (and that I initially had overread), first set the missing values to 0, then divide: 然后执行你要求的除法(我最初有过读),首先将缺失值设置为0,然后除以:

tmp[is.na(tmp[,"ili"]),"ili"] <- 0                                                                                                                    
tmp[is.na(tmp[,"no.ili"]),"no.ili"] <- 0                                                                                                              

res <- tmp[,"ili"]/tmp[,"no.ili"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM