0

我有一个名为 的数据表dat,如下所示:

               Alison.Wong Bruno.Dumon Edward.J..Yoon Eugene.Koontz Jakob.Homan
Alison Wong              0           0              0             1           1
Bruno Dumon              0           0              0             0           1
Edward J. Yoon           0           1              0             0           0
Eugene Koontz            0           0              0             0           1
Jakob Homan              1           0              1             0           0

输入(头(数据)

structure(list(Alison.Wong = c(0L, 0L, 0L, 0L, 1L), Bruno.Dumon = c(0L, 
0L, 1L, 0L, 0L), Edward.J..Yoon = c(0L, 0L, 0L, 0L, 1L), Eugene.Koontz = c(1L, 
0L, 0L, 0L, 0L), Jakob.Homan = c(1L, 1L, 0L, 1L, 0L)), .Names = c("Alison.Wong", 
"Bruno.Dumon", "Edward.J..Yoon", "Eugene.Koontz", "Jakob.Homan"
), row.names = c("Alison Wong", "Bruno Dumon", "Edward J. Yoon", 
"Eugene Koontz", "Jakob Homan"), class = "data.frame")

如何通过保留模板中列的顺序以及保留输入文件 ( dat) 中的数据,将上面的表格(包含数据)与其他 .csv 模板结合起来,并以其他名称保存。

示例模板文件:

                Adrian.Cole Alison.Wong Andrei.Savu Bruno.Dumon Edward.J..Yoon
Adrian Cole               0           0           0           0              0
Alison Wong               0           0           0           0              0
Andrei Savu               0           0           0           0              0
Bruno Dumon               0           0           0           0              0
Edward J. Yoon            0           0           0           0              0
Eugene Koontz             0           0           0           0              0
Jakob Homan               0           0           0           0              0
Kelvin Kakugawa           0           0           0           0              0
                Eugene.Koontz Jakob.Homan Kelvin.Kakugawa
Adrian Cole                 0           0               0
Alison Wong                 0           0               0
Andrei Savu                 0           0               0
Bruno Dumon                 0           0               0
Edward J. Yoon              0           0               0
Eugene Koontz               0           0               0
Jakob Homan                 0           0               0
Kelvin Kakugawa             0           0               0

输入(头(模板)):

structure(list(Adrian.Cole = c(0L, 0L, 0L, 0L, 0L, 0L), Alison.Wong = c(0L, 
0L, 0L, 0L, 0L, 0L), Andrei.Savu = c(0L, 0L, 0L, 0L, 0L, 0L), 
    Bruno.Dumon = c(0L, 0L, 0L, 0L, 0L, 0L), Edward.J..Yoon = c(0L, 
    0L, 0L, 0L, 0L, 0L), Eugene.Koontz = c(0L, 0L, 0L, 0L, 0L, 
    0L), Jakob.Homan = c(0L, 0L, 0L, 0L, 0L, 0L), Kelvin.Kakugawa = c(0L, 
    0L, 0L, 0L, 0L, 0L)), .Names = c("Adrian.Cole", "Alison.Wong", 
"Andrei.Savu", "Bruno.Dumon", "Edward.J..Yoon", "Eugene.Koontz", 
"Jakob.Homan", "Kelvin.Kakugawa"), row.names = c("Adrian Cole", 
"Alison Wong", "Andrei Savu", "Bruno Dumon", "Edward J. Yoon", 
"Eugene Koontz"), class = "data.frame")

此示例的示例输出:

                Adrian.Cole Alison.Wong Andrei.Savu Bruno.Dumon Edward.J..Yoon
Adrian Cole               0           0           0           0              0
Alison Wong               0           0           0           0              0
Andrei Savu               0           0           0           0              0
Bruno Dumon               0           0           0           0              0
Edward J. Yoon            0           0           0           1              0
Eugene Koontz             0           0           0           0              0
Jakob Homan               0           1           0           0              1
Kelvin Kakugawa           0           0           0           0              0
                Eugene.Koontz Jakob.Homan Kelvin.Kakugawa
Adrian Cole                 0           0               0
Alison Wong                 1           1               0
Andrei Savu                 0           0               0
Bruno Dumon                 0           1               0
Edward J. Yoon              0           0               0
Eugene Koontz               0           1               0
Jakob Homan                 0           0               0
Kelvin Kakugawa             0           0               0

我尝试使用以下脚本,但它不起作用。

template<- read.table("CR_template.csv", header=T, sep=",", row.names=1)
    template[match(rownames(cr), rownames(template)) , names(cr)] <- cr[ , names(cr)]
    result <- merge(cr, template)
    write.csv(result, paste("CR_FILES_", a.files[i], sep=""))
4

1 回答 1

2

使用您提供的示例,这非常简单:

template[ , names(dat)] <- dat[ , names(dat)]

但是,您发布的数据与您为“dat”和“模板”报告的(方形)维度不匹配。所以我建议你发布一个小得多的例子dat,比如 5 x 5,和一个稍微大一点的“模板”例子,比如 8 x 8,并发布你认为正确答案应该是什么。

如果需要对列和行进行交错,那么沿着这些思路可能会成功,但目前我认为它未经测试:

template[match(rownames(dat), rownames(template)) , names(dat)] <- dat[ , names(dat)]

对于编辑后的版本,这有效:

datrows <- match(rownames(template), rownames(dat) )
transfer <- dat[ datrows[!is.na(datrows)], ]
template[rownames(transfer) , colnames(transfer) ] <- transfer

> template
               Adrian.Cole Alison.Wong Andrei.Savu Bruno.Dumon Edward.J..Yoon Eugene.Koontz Jakob.Homan
Adrian Cole              0           0           0           0              0             0           0
Alison Wong              0           0           0           0              0             1           1
Andrei Savu              0           0           0           0              0             0           0
Bruno Dumon              0           0           0           0              0             0           1
Edward J. Yoon           0           0           0           1              0             0           0
Eugene Koontz            0           0           0           0              0             0           1
               Kelvin.Kakugawa
Adrian Cole                  0
Alison Wong                  0
Andrei Savu                  0
Bruno Dumon                  0
Edward J. Yoon               0
Eugene Koontz                0
于 2012-09-26T02:13:48.150 回答