2015-12-26 39 views
0

我想基于破裂参数条件语句来cut我的数据,但它抛出这个错误:如何使用条件语句中切

Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : length of 'dimnames' [2] not equal to array extent

是否可以使用条件语句这样在cut里面?

示例数据

df <- structure(list(fyear = c(1970, 1970, 1970, 1970, 1970, 1970, 
1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 1970, 
1970, 1970, 1970), BEME = c(0.39713747645951, 0.548988782444936, 
0.537154930871343, 1.89357008340059, 1.66945262543448, 0.969181836638018, 
1.09989952916609, 0.858308443214104, 0.292175536881419, 0.684685677549708, 
0.338422675433708, 3.02671555788371, 0.422643864469658, 0.805317430736738, 
0.529954031556715, 0.617716486520065, 0.911576593365635, 0.4131850675139, 
1.16211278792693, 2.13177678851802), exchg = c(11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 11L, 11L, 12L, 11L, 12L, 
19L, 11L, 11L, 11L)), .Names = c("fyear", "BEME", "exchg"), class = c("tbl_df", 
"data.frame"), row.names = c(NA, -20L)) 

Cut功能

cut(df$BEME, breaks = quantile(df[df$exchg == 11, 2], c(0,0.3,0.7,1)), labels = FALSE) 

回答

1

cut(df$BEME, breaks = quantile(df[df$exchg == 11, 2]$BEME, c(0,0.3,0.7,1)), labels = FALSE) 

变化

df[df$exchg == 11, 2] 

df[df$exchg == 11, 2]$BEME 

的第一项返回data.frame第二向量(这是你想要的)。