我有一个名为 的数据表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=""))