在R中组织列和行

时间:2017-08-25 17:07:19

标签: r

我有大量的财务报表导入R,需要通过下面的年份(YN)和下面的金额(vN)汇总帐户名称(下例中的VarN)。我使用reshape和dplyr搜索了解决方案,但无济于事。 我有:

for (i in 1:64) {
    if (sum(is.na(prop_train$nam[i]))/length(prop_train$nam[i]) > .3) {
        prop_train$nam[i] <- NULL
    }
}     

我需要转换为:

error:Error in if (sum(is.na(prop_train$nam[i]))/length(prop_train$nam[i]) >  :    missing value where TRUE/FALSE needed In addition: Warning message: In is.na(prop_train$nam[i]) :

1 个答案:

答案 0 :(得分:0)

在数据帧df上使用重塑库: http://www.statmethods.net/management/reshape.html

library(reshape)

column1 <- c("Var1", "Var2", "Var3", "Var1", "Var2", "Var3")
column2 <- c("Y1", "Y1", "Y1", "Y2", "Y2", "Y2")
column3 <- c("v1", "v2", "v3", "v4", "v5", "v6")


df <- data.frame(column1, column2, column3)
x <- c("Var", "y", "v")
colnames(df) <- x

mdata <- melt(df, id=c("Var", "y"))
cast(mdata, Var ~ y, value = 'v')

不要忘记先熔化数据。