使用data.table将data.frame行转换为列名
我有一个500万行的大数据框,有三列。我想把它转换成一个矩阵,它的行是USER_ID,ID是columns,value是CNT。这可以通过熔化和浇铸或 但是,创建的对象太大,我得到以下错误“dim”指定数组太大 我尝试使用data.table原样来处理大数据,比data.frame好得多,但我不知道如何使用data.table创建我想要的列联表。使用data.table将data.frame行转换为列名,r,dataframe,data.table,R,Dataframe,Data.table,我有一个500万行的大数据框,有三列。我想把它转换成一个矩阵,它的行是USER_ID,ID是columns,value是CNT。这可以通过熔化和浇铸或 但是,创建的对象太大,我得到以下错误“dim”指定数组太大 我尝试使用data.table原样来处理大数据,比data.frame好得多,但我不知道如何使用data.table创建我想要的列联表。 有人知道如何让它工作吗?我也在考虑用适当的维度创建并清空矩阵,并用适当的索引填充它。使用内置的数据框尝试此操作。框架: 或类似地使用tapply: 现
有人知道如何让它工作吗?我也在考虑用适当的维度创建并清空矩阵,并用适当的索引填充它。使用内置的数据框尝试此操作。框架: 或类似地使用tapply: 现在与data.table进行比较:
> library(data.table)
>
> DT <- data.table(CO2)
> DT[, as.list(tapply(uptake, Type, sum)), by = Treatment]
Treatment Quebec Mississippi
1: nonchilled 742.0 545.0
2: chilled 666.8 332.1
上面的注意事项也适用于此处。使用内置的data.frame CO2尝试此操作: 或类似地使用tapply: 现在与data.table进行比较:
> library(data.table)
>
> DT <- data.table(CO2)
> DT[, as.list(tapply(uptake, Type, sum)), by = Treatment]
Treatment Quebec Mississippi
1: nonchilled 742.0 545.0
2: chilled 666.8 332.1
上面的注意事项也适用于这里。这里还有另一种使用CO2数据集的方法:
这里还有另一种使用CO2数据集的方法:
您好,欢迎来到苏。你的起点和终点都不清楚。你贴的那张小桌子是哪张?另外,请尝试粘贴dputheadHello的输出,欢迎使用SO。你的起点和终点都不清楚。你贴的那张小桌子是哪张?另外,请尝试粘贴dputhead的输出
> xtabs(uptake ~ Treatment + Type, CO2)
Type
Treatment Quebec Mississippi
nonchilled 742.0 545.0
chilled 666.8 332.1
> with(CO2, tapply(uptake, list(Treatment, Type), sum))
Quebec Mississippi
nonchilled 742.0 545.0
chilled 666.8 332.1
> library(data.table)
>
> DT <- data.table(CO2)
> DT[, as.list(tapply(uptake, Type, sum)), by = Treatment]
Treatment Quebec Mississippi
1: nonchilled 742.0 545.0
2: chilled 666.8 332.1
> DT[, setNames(as.list(.SD[,list(uptake = sum(uptake)), by = Type][, uptake]),
+ levels(Type)), by = Treatment]
Treatment Quebec Mississippi
1: nonchilled 742.0 545.0
2: chilled 666.8 332.1
dt = data.table(CO2)
dt[, sum(uptake), by = list(Treatment, Type)][,
setNames(as.list(V1), paste(Type)), by = Treatment]
# Treatment Quebec Mississippi
#1: nonchilled 742.0 545.0
#2: chilled 666.8 332.1