如何在R中动态组合具有不同列名称的数据框?

时间:2019-04-21 19:17:00

标签: r

我有一个分析脚本,该脚本处理结构相似但列名称不同的数据批次。我需要保留列名称以供以后的ETL脚本使用,但是我们想做一些处理,例如:

results <- data.frame();
for (name in names(data[[1]])) {   
    # Start by combining each column into a single matrix
    working <- lapply(data, function(item)item[[name]]);
    working <- matrix(unlist(working), ncol = 50, byrow = TRUE);

    # Dump the data for the archive
    write.csv(working, file = paste(PATH, prefix, name, '.csv', sep = ''), row.names = FALSE);

    # Calculate the mean and SD for each year, bind to the results
    df <- data.frame(colMeans(working), colSds(working));
    names(df) <- c(paste(name, '.mean', sep = ''), paste(name, '.sd', sep = ''));

    # Combine the working df with the processing one
}

根据示例中的最后一条注释,如何合并数据帧?我已经尝试过rbindrbind.fill,但都没有用,它们可能是数据文件中不同列名的10到100。

1 个答案:

答案 0 :(得分:0)

在搜索正确的关键字时,这可能更成问题,但是void overRC(struct list *z) { static int mdays[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int deleted = 0; struct data **linkp = &z->first; struct data *temp; while ((temp = *linkp) != NULL) { int number = temp->ID / 10000; int day = number % 100; int month = number / 100 % 100; int year = 1900 + number / 10000 % 100; if (temp->ID % 11 != 0 || month <= 0 || month > 12 || day <= 0 || day > mdays[month] || (month == 2 && day == 29 && (year == 1900 || year % 4 != 0))) { *linkp = temp->next; free(temp); deleted++; } else { linkp = &temp->next; } } printf("Deleted %d nodes\n", deleted); } 方法实际上是与矩阵配合使用的方式,

cbind

显然,要注意的是各列必须具有相同的行数,否则,只需将各列附加到矩阵即可。