2013-01-02 48 views
0

下的比较,我有一个小问题, 我有这些参数:2个条件

df <- data.frame(Equip = c(1,1,1,1,1,2,2,2,2,2), 
       Notif = c(1,1,1,2,2,3,3,3,3,4), 
       Component = c("Dichtung","Motor","Getriebe","Service","Motor","Lüftung","Dichtring","Motor","Getriebe","Dichtring"), 
       rank= c(1 , 1 , 1 , 2 , 2 , 1 , 1 , 1 , 1 , 2)) 

现在我想有一个比较,只是为了一个Equip看,如果在第一rank的使用Components,是一样的,在第二rank(只是由同一Equip):

在2种方式:

第一:是所有的组件一样的吗?

任何(最少1个)组件是否相同?

我需要一个高度自动的解决方案,因为我的数据集有超过150k行。

想要的答案可能是一个只有布尔表达式的向量,包括TRUE和FALSE。

因此,对于上面的例子,

answer <- c(TRUE,TRUE)

由于装备1个秩1成分:电机 “AND” 装备1个秩2是组件:电机为好。 (对于1种期望的方式为例)

非常感谢您的帮助=)


我使用的评论功能,但我不能显示的问题,因为我想显示的代码。

请遗憾的是..

原始数据有更多然后2点,现在我想排名X秩X +在一个步骤1相结合,这是一个使用此我使用的福尔​​循环在功能,但它不工作任何想法?

a <- lapply(split(df,df$Equips),function(x){ 
for(i in 1:8){ 
    ll <- split(x,x$rank) 
if(length(ll)>i) 
ii <- intersect(ll[[i]]$Comps,ll[[i+1]]$Comps) 
else ii <- NA c(length(ii)> 0 && !is.na(ii),ii) 
} 
}) 
b <- unlist(a) 
c <- table(b,b) 
rowSums(c) 

任何想法,我能为它(做的主要思想是有1-2,2-3,3-4等结果一步到位

+0

什么是你想要的结果吗?您能否使用您发布的数据向我们展示一个示例? – digEmAll

+0

对于相同的装备(例如= 1),如果一个等级中的其中一个成分在另一个等级中重复,那么您希望结果=真?那么,每个Equip值只有一个布尔值? – digEmAll

+0

是的,这是我想要的第一步。第二个是如果所有组件都是相同的。但你明白我的意思,是的。 – Daniel

回答

0

plyr适用于基操作

dat.r <- dlply(df ,.(Equip),function(x){  # I split by Equipe 
    ll <- split(x,x$rank)      # I split by rank 

    if(length(ll)> 1) 
    ii <- intersect(ll[[1]]$Comps,ll[[2]]$Comps) ## test intersection 
    else 
    ii <- NA 
    c(length(ii)> 0 && !is.na(ii),ii)      ## the result 
}) 

在这里我得到了comparaison结果和组件名称

dat.r 
$`1` 
[1] "TRUE" "Motor" 

编辑:这里的结果与基本包(没有互联网)

lapply(split(df,df$Equip),function(x){  # I split by Equipe 
    ll <- split(x,x$rank)      # I split by rank 
    if(length(ll)> 1) 
    ii <- intersect(ll[[1]]$Comps,ll[[2]]$Comps) ## test intersection 
    else 
    ii <- NA 
    c(length(ii)> 0 && !is.na(ii),ii)           ## the result 
}) 

$`1` 
[1] "TRUE" "Motor" 

$`2` 
[1] "TRUE"  "Dichtring" 
+0

你到底意味着什么?获取“再次使用”组件信息的想法非常有用。如果你的意思是这个,我想获得这些信息。就像我在我的主题中讲的那样,它是一个大数据集,我必须总结一下,比如查找经常使用哪个组件,哪些不是。喜欢这个。它是我分析的第一步。 – Daniel

+0

数据的结构与给定示例中的结构相同,只有两个不同的东西,还有两个等级(但是我测试了它,这不是它不起作用的原因),另一个是,数据集非常大。我也认为这是一个很好的解决方案(很难看)。还有另一种可能性吗?真的非常感谢你的帮助=) – Daniel

+0

我真的不知道该怎么做,仍然有错误代码:“下标越界”会有一大堆向量能帮助你吗? rle(df $ rank) 运行长度编码 长度:int [1:8750] 5 1 4 34 2 29 1 8 1 8 ... values:int [1:8750] 1 2 3 1 2 1 2 1 2 1 ... > rle(df $ Equips) 运行长度编码 长度:int [1:20268] 1 2 7 1 1 1 3 2 2 2 ... values:int [1:20268] 10024820 10025441 10040292 10045800 10050420 10062984 10071240 10083675 ​​10090325 10099139 ... – Daniel

0

这里是一个可能的解决方案:

df <- data.frame(Equip = c(1,1,1,1,1,2,2,2,2,2), 
       Notif = c(1,1,1,2,2,3,3,3,3,4), 
       Component = c("Dichtung","Motor","Getriebe","Service","Motor","Lüftung","Dichtring","Motor","Getriebe","Dichtring"), 
       rank= c(1 , 1 , 1 , 2 , 2 , 1 , 1 , 1 , 1 , 2)) 


allComponents <- function(subDf){ 
    setequal(subDf[subDf$rank==1,'Component'],subDf[subDf$rank==2,'Component']) 
} 

anyComponents <- function(subDf){ 
    length(intersect(subDf[subDf$rank==1,'Component'],subDf[subDf$rank==2,'Component'])) > 0 
} 

# all components are equal 
res1 <- by(df,INDICES=df$Equip,FUN=allComponents) 
# at least one component equal 
res2 <- by(df,INDICES=df$Equip,FUN=anyComponents) 

as.vector(res1) 
> FALSE, FALSE 

as.vector(res2) 
> TRUE, TRUE