I have a table with data named dat, as below:
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
dput(head(dat)
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")
How can I combine the table above (which contains data) with other .csv template by preserving the order of the column in the template and also by preserving the data from the input file (dat), and save it with other name.
sample template file:
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
dput(head(template)):
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")
sample output from this example:
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
I tried to use the following script, but it doesnt work.
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=""))
With the example you offered, it is incredibly easy:
However, what you posted as data does not match up with the (square) dimensions you are reporting for both ‘dat’ and ‘template’. So I would suggest you post a much smaller example for
dat, say 5 x 5, and a slightly bigger example for ‘template’, say 8 x 8, and also post what you think the right answer should be.If there needs to be interleaving of both columns and rows then it’s possble something along these lines will succeed, but at the moment I consider it untested:
For the edited version, this works: