我有一个类似于以下示例所示的大型数据集。
df <- structure(list(FCN = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L,
3L), .Label = c("010.X91116.3D3.A8", "010.X91116.6B7.F9", "010.X91116.6C6.C12"
), class = "factor"), DOM = structure(c(1L, 2L, 2L, 1L, 2L, 1L,
2L, 2L), .Label = c("VH", "VK"), class = "factor"), FN = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "OM", class = "factor"),
RV = c(49257.4, 23571.2, 24115.6, 49351.4, 24102.6, 49641.8,
23226.2, 23408.2)), .Names = c("FCN", "DOM", "FN", "RV"), class = "data.frame", row.names = c(NA,
-8L))
我正在尝试创建一个表,方法是根据FCN,RV和DOM中的值使FN列值具有唯一的后缀。我能够使用for循环并处理数据,如图所示。但是,处理数千行需要时间。
最后,我想转动数据,使FN中的值成为RV中的列和值。如果可能的话,请指导我如何使用一些库函数优雅地实现结果。
library(reshape2)
pivot_df <- dcast(df, FCN + DOM ~ FN)
答案 0 :(得分:2)
我们可以使用ave
来执行此操作
df$FN <- with(df, paste0(FN, ave(seq_along(FN), FCN, DOM, FUN = seq_along)))
如果我们需要重塑为“广角”,那么rowid
中的data.table
可以与dcast
一起使用
library(data.table)
dcast(setDT(df), FCN + DOM ~FN + rowid(DOM), value.var = "RV")
答案 1 :(得分:1)
采纳@ akrun的建议:
library(reshape2)
df <- structure(list(FCN = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L), .Label = c("010.X91116.3D3.A8", "010.X91116.6B7.F9", "010.X91116.6C6.C12"), class = "factor"), DOM = structure(c(1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L), .Label = c("VH", "VK"), class = "factor"), FN = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "OM", class = "factor"), RV = c(49257.4, 23571.2, 24115.6, 49351.4, 24102.6, 49641.8, 23226.2, 23408.2)), .Names = c("FCN", "DOM", "FN", "RV"), class = "data.frame", row.names = c(NA, -8L))
df$FN <- with(df, paste0(FN, ave(seq_along(FN), FCN, DOM, FUN = seq_along)))
pivot_df <- dcast(df, FCN + DOM ~ FN)