Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.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
大型数据帧的dcast[R]_R_Reshape_Large Data - Fatal编程技术网

大型数据帧的dcast[R]

大型数据帧的dcast[R],r,reshape,large-data,R,Reshape,Large Data,假设DF为: pnr <- c(1, 1, 1, 2, 2, 3, 4, 5, 5) diag <- c("a", "a", NA, "b", "a", NA, "c", "a", "f") year <- rep(2007, 9) ht <- data.frame(pnr, diag, year) pnr解决这个问题的简单方法是切换回旧的重塑包。这意味着使用cast而不是dcast。阿伦的评论是非常有用的,只要你能真正更新。 您可以尝试

假设DF为:

    pnr <- c(1, 1, 1, 2, 2, 3, 4, 5, 5)
    diag <- c("a", "a", NA, "b", "a", NA, "c", "a", "f")
    year <- rep(2007, 9)
    ht <- data.frame(pnr, diag, year)

pnr解决这个问题的简单方法是切换回旧的重塑包。这意味着使用cast而不是dcast。阿伦的评论是非常有用的,只要你能真正更新。

您可以尝试使用
dplyr/tidyr
函数
collect/spread
或将data.frame转换为
data.table
并使用
dcast.data.table
。我希望它能起作用。此外,您不需要
as.data.frame(cbind(
),只需
data.frame(
)就足够了。前者将把所有列转换为字符,因为
cbind
获得矩阵输出,矩阵只能有一个类。在您的数据中,也有
character
列。请尝试
库(data.table);dcast.data.table(melt(setDT(ht),id=c('pnr','year')),pnr~value)
无需
melt
此处:
dcast.data.table(setDT(ht),pnr~diag,value.var=“diag”)
应该足够了。找不到dcast.data.table,我想我有一个旧版本。在服务器上运行,我想我在任何时候都看不到更新,因为您可以在本地目录上安装软件包。在StackOverflow上搜索如何在本地安装软件包。
    require(reshape2)
    md <- melt(ht, id = c("pnr", "year"))
    output <- dcast(md, pnr ~ value)