Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 对数据帧进行子集设置';s列由it';s相应的级别,并对每个子集应用函数_R_Dataframe_Group By_Lapply_Quantile - Fatal编程技术网

R 对数据帧进行子集设置';s列由it';s相应的级别,并对每个子集应用函数

R 对数据帧进行子集设置';s列由it';s相应的级别,并对每个子集应用函数,r,dataframe,group-by,lapply,quantile,R,Dataframe,Group By,Lapply,Quantile,当我的值在“short_desc”列中只有一个因子级别时,我有一些代码可以工作。它获取“Value”并在“Value.fc”列中输出相应的小数范围 操作前的data.frame如下所示: 我将这些代码行应用于数据帧“df”: 请使用dput显示示例数据,而不是图像输出(head(df))结构(列表(state=c(“爱荷华州”、“爱荷华州”、“伊利诺伊州”),简称c(“玉米、谷物-产量,单位为bu/英亩”,“玉米、青贮饲料-产量,单位为吨/英亩”,“玉米、谷物-产量,单位为bu/英亩”),值=

当我的值在“short_desc”列中只有一个因子级别时,我有一些代码可以工作。它获取“Value”并在“Value.fc”列中输出相应的小数范围

操作前的data.frame如下所示:

我将这些代码行应用于数据帧“df”:


请使用
dput
显示示例数据,而不是图像输出(head(df))结构(列表(state=c(“爱荷华州”、“爱荷华州”、“伊利诺伊州”),简称c(“玉米、谷物-产量,单位为bu/英亩”,“玉米、青贮饲料-产量,单位为吨/英亩”,“玉米、谷物-产量,单位为bu/英亩”),值=c(137.8,13.5,153.3),FIPS=c(“19001”,“19001”、“17001”,Value.fc=c(“135-0-150”、“13-0-14-5”、“150-4-157”))
df <- df %>%
mutate(Value.fc = cut2(Value, g=10),
     Value.fc = factor(sapply(str_extract_all(Value.fc, "\\d+"),
                              function(x) paste(x, collapse="-"))),
     Value.fc = reorder(Value.fc, Value)) 
df <- df %>%
group_by(short_desc) %>%
mutate(Value.fc = cut2(Value, g=10),
     Value.fc = factor(sapply(str_extract_all(Value.fc, "\\d+"),
                              function(x) paste(x, collapse="-"))),
     Value.fc = reorder(Value.fc, Value)) %>%
ungroup()
dput(head(df)) structure(list(state = c("Iowa", "Iowa", "Illinois"), short_desc = c("Corn, grain - yield, measured in bu / acre", "Corn, silage - yield, measured in tons / acre", "Corn, grain - yield, measured in bu / acre"), Value = c(137.8, 13.5, 153.3), FIPS = c("19001", "19001", "17001"), Value.fc = c("135-0-150", "13-0-14-5", "150-4-157"))