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

如何在R中动态组合具有不同列名的数据帧?,r,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]]); work

我有一个分析脚本,它处理具有类似结构但不同列名的批量数据。我需要为以后的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
}

results在搜索正确的关键字时,这可能是一个更大的问题,但是
cbind
方法实际上是与矩阵一起使用的方法

# Allocate for the number of rows needed
results <- matrix(nrow = rows)

for (name in names(data[[1]])) {   
    # Data processing

    # Append the results to the working data
    results <- cbind(results, df)   
}

# Drop the first placeholder column created upon allocation
results <- results[, -1];
#分配所需的行数

结果在搜索正确的关键词时,这可能是一个更大的问题,但是
cbind
方法实际上是一个矩阵

# Allocate for the number of rows needed
results <- matrix(nrow = rows)

for (name in names(data[[1]])) {   
    # Data processing

    # Append the results to the working data
    results <- cbind(results, df)   
}

# Drop the first placeholder column created upon allocation
results <- results[, -1];
#分配所需的行数

结果你能发布一些我们可以用来重现你的错误的数据吗?因为在我的计算机上,我创建了两个具有不同列名的虚拟数据帧,并且
rbind.fill
工作得很好。@nsinghs我怀疑是列名造成了这个问题。您几乎可以为列c('a'、'B'、…、'Z')生成一组随机值,这与文件类似。当我运行rbind.fill时,将前两列填入fine(例如,'A.mean','A.sd'),但其余的列填入NA以表示数据。然而,在执行过程中进行的检查显示生成了有效的数据。您能否发布一些数据,我们可以用来重现您的错误?因为在我的计算机上,我创建了两个具有不同列名的虚拟数据帧,并且
rbind.fill
工作得很好。@nsinghs我怀疑是列名造成了这个问题。您几乎可以为列c('a'、'B'、…、'Z')生成一组随机值,这与文件类似。当我运行rbind.fill时,将前两列填入fine(例如,'A.mean','A.sd'),但其余的列填入NA以表示数据。但是,执行期间的检查显示生成了有效数据。