【问题标题】:How to use a conditional statement in cut如何在cut中使用条件语句
【发布时间】:2015-12-26 21:12:23
【问题描述】:

我想cut我的数据基于 break 参数中的条件语句,但它抛出了这个错误:

矩阵错误(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)

【问题讨论】:

    标签: r


    【解决方案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,第二个返回一个向量(这是你想要的)。

    【讨论】:

      猜你喜欢
      • 2011-06-21
      • 2021-02-22
      • 2018-04-04
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多