我希望根据break参数中的条件语句cut
我的数据,但它会抛出此错误:
矩阵错误(unlist(value,recursive = FALSE,use.names = FALSE), nrow = nr,:dimnames的长度' [2]不等于数组范围
是否可以这样在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)
答案 0 :(得分:2)
在
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第二个向量(这是你想要的)。