[R] Write table with data in other .csv template

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Wed Sep 26 04:29:34 CEST 2012


CSV files are on disk, with filenames. You can call them whatever you want, but they are not variables in R.

Data frames are variables in memory. You can read any CSV file into any data frame you like, but it replaces the original contents of the data frame completely. You can also write a data frame into a CSV file, but that always overwrites the entire file on disk. You may want to avoid writing over your input files, instead create a new output file.

You cannot manipulate CSV files in place using normal R programming techniques. You must load them into data frames, manipulate them, and write the result back to disk.

I used the name "template" because you did. You can use different names if you wish.

 I strongly recommend that you read "An Introduction to R" until you understand the syntax I used and can re-create it yourself using your own variables. Blindly using any code without understanding it is risky to your data.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

"s.s.m. fauzi" <ssmfone at gmail.com> wrote:

>Hi Jeff,
>Do I need to call the .csv template to perform this operation?
>
>Shukor
>
>On Wed, Sep 26, 2012 at 11:50 AM, Jeff Newmiller
><jdnewmil at dcn.davis.ca.us>wrote:
>
>> Perhaps you might try
>>
>> template[ , names(tbl) ] <- tbl
>>
>> where I have used tbl instead of table, for reasons previously
>mentioned.
>>
>---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go
>Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#. 
>LiveGo...
>>                                       Live:   OO#.. Dead: OO#.. 
>Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#. 
>rocks
>> ...1k
>>
>---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>> "s.s.m. fauzi" <ssmfone at gmail.com> wrote:
>>
>> >Hi Jeff,
>> >Thank you for the respond.
>> >
>> >I'm not really familiar with cbind.
>> >
>> >Anyway, there is a duplication in my input and the template, and I
>want
>> >to
>> >preserve the column order.
>> >
>> >Shukor
>> >
>> >On Wed, Sep 26, 2012 at 11:30 AM, Jeff Newmiller
>> ><jdnewmil at dcn.davis.ca.us>wrote:
>> >
>> >> Thanks for posting dput data, but I don't understand your
>question.
>> >>
>> >> are you familiar with cbind?
>> >> Is there duplication of column names in your input table and
>> >template? Are
>> >> they always duplicated in the template? What characteristics of
>the
>> >> template are you trying to preserve? Column order?
>> >>
>> >> Other:
>> >> Don't post HTML email.
>> >> The name "table" is defined in the base package, so using it as a
>> >data
>> >> name can lead to confusion and inability to perform fundamental
>> >operations.
>> >>
>>
>>
>>---------------------------------------------------------------------------
>> >> Jeff Newmiller                        The     .....       ..... 
>Go
>> >Live...
>> >> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#. 
>Live
>> >> Go...
>> >>                                       Live:   OO#.. Dead: OO#..
>> >Playing
>> >> Research Engineer (Solar/Batteries            O.O#.       #.O#. 
>with
>> >> /Software/Embedded Controllers)               .OO#.       .OO#.
>> >rocks...1k
>> >>
>>
>>
>>---------------------------------------------------------------------------
>> >> Sent from my phone. Please excuse my brevity.
>> >>
>> >> "s.s.m. fauzi" <ssmfone at gmail.com> wrote:
>> >>
>> >> >Hi,
>> >> >
>> >> >I have a table with data, as below:
>> >> >
>> >> >dput(table):
>> >> >
>> >> >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), Kirk.True = c(0L, 0L, 0L, 0L, 0L, 0L
>> >> >  ), Lars.George = c(0L, 0L, 1L, 0L, 0L, 0L), Soren.Macbeth =
>c(0L,
>> >0L,
>> >> >1L, 0L, 0L, 0L), Stu.Hood = c(0L, 0L, 0L, 0L, 0L, 0L),
>> >> >  Tibor.Kiss = c(0L, 0L, 0L, 0L, 0L, 0L), Tom.White = c(0L, 0L,
>1L,
>> >0L,
>> >> >0L, 0L), Unassigned = 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", "Kirk.True", "Lars.George", "Soren.Macbeth",
>> >> >"Stu.Hood",
>> >> >"Tibor.Kiss", "Tom.White", "Unassigned"), row.names = c("Adrian
>> >Cole",
>> >> >"Alison Wong", "Andrei Savu", "Bruno Dumon", "Edward J. Yoon",
>> >"Eugene
>> >> >Koontz"), class = "data.frame")
>> >> >
>> >> >> dim(table)
>> >> >[1] 15 15
>> >> >> names(table)
>> >> >[1] "Adrian.Cole"     "Alison.Wong"     "Andrei.Savu"
>> >"Bruno.Dumon"
>> >> >"Edward.J..Yoon"  "Eugene.Koontz"   "Jakob.Homan"
>> >"Kelvin.Kakugawa"
>> >> > [9] "Kirk.True"       "Lars.George"     "Soren.Macbeth"
>> >"Stu.Hood"
>> >> > "Tibor.Kiss"      "Tom.White"       "Unassigned"
>> >> >
>> >> >What I want to do is to embed/write this table to other big .csv
>> >> >template,
>> >> >and save it with other name.
>> >> >
>> >> >dput(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), Kirk.True = c(0L, 0L, 0L, 0L, 0L, 0L
>> >> >  ), Lars.George = c(0L, 0L, 0L, 0L, 0L, 0L), Soren.Macbeth =
>c(0L,
>> >0L,
>> >> >0L, 0L, 0L, 0L), Stu.Hood = c(0L, 0L, 0L, 0L, 0L, 0L),
>> >> >  Tibor.Kiss = c(0L, 0L, 0L, 0L, 0L, 0L), Tom.White = c(0L, 0L,
>0L,
>> >0L,
>> >> >0L, 0L), Unassigned = c(0L, 0L, 0L, 0L, 0L, 0L
>> >> >   ), David.Alves = c(0L, 0L, 0L, 0L, 0L, 0L), Patrick.Hunt =
>c(0L,
>> >0L,
>> >> >0L, 0L, 0L, 0L), Sebastian.Schoenherr = c(0L, 0L, 0L,
>> >> >0L, 0L, 0L), Roman.Valls = c(0L, 0L, 0L, 0L, 0L, 0L),
>> >Pavel.Yaskevich =
>> >> >c(0L, 0L, 0L, 0L, 0L, 0L), Karel.Vervaeke = c(0L, 0L, 0L, 0L, 0L,
>> >> >  0L), Shay.Banon = c(0L, 0L, 0L, 0L, 0L, 0L), Hyunsik.Choi =
>c(0L,
>> >0L,
>> >> >0L, 0L, 0L, 0L), Joey.Hung. = c(0L, 0L, 0L, 0L, 0L, 0L
>> >> >   ), Tommaso.Teofili = c(0L, 0L, 0L, 0L, 0L, 0L),
>> >Jeff.Hammerbacher. =
>> >> >c(0L, 0L, 0L, 0L, 0L, 0L), Andrew.Purtell. = c(0L, 0L, 0L, 0L,
>> >> >0L, 0L), Johan.Oskarsson = c(0L, 0L, 0L, 0L, 0L, 0L), Erik.Frey =
>> >c(0L,
>> >> >0L, 0L, 0L, 0L, 0L), Sebb = c(0L, 0L, 0L, 0L, 0L, 0L),
>> >Andrew.klochkov
>> >> >=
>> >> >c(0L,
>> >> >   0L, 0L, 0L, 0L, 0L), Paul.Egan = c(0L, 0L, 0L, 0L, 0L, 0L),
>> >Hudson =
>> >> >c(0L, 0L, 0L, 0L, 0L, 0L), Hadoop = c(0L, 0L,
>> >> >  0L, 0L, 0L, 0L), Aaron = c(0L, 0L, 0L, 0L, 0L, 0L), henry =
>c(0L,
>> >0L,
>> >> >0L, 0L, 0L, 0L), steve = c(0L, 0L, 0L, 0L, 0L, 0L), russel =
>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", "Kirk.True", "Lars.George",
>> >> >"Soren.Macbeth",
>> >> >"Stu.Hood", "Tibor.Kiss", "Tom.White", "Unassigned",
>> >> >"David.Alves", "Patrick.Hunt", "Sebastian.Schoenherr",
>> >> >"Roman.Valls", "Pavel.Yaskevich", "Karel.Vervaeke", "Shay.Banon",
>> >> >"Hyunsik.Choi",
>> >> >"Joey.Hung.", "Tommaso.Teofili", "Jeff.Hammerbacher.",
>> >> >"Andrew.Purtell.", "Johan.Oskarsson", "Erik.Frey", "Sebb",
>> >> >"Andrew.klochkov", "Paul.Egan",
>> >> >"Hudson", "Hadoop", "Aaron", "henry", "steve", "russel"),
>row.names
>> >=
>> >> >c("Adrian Cole", "Alison Wong", "Andrei Savu", "Bruno Dumon",
>> >"Edward
>> >> >J.
>> >> >Yoon",
>> >> >"Eugene Koontz"), class = "data.frame")
>> >> >
>> >> >> dim(template)
>> >> >[1] 38 38
>> >> >> names(template)
>> >> > [1] "Adrian.Cole"          "Alison.Wong"          "Andrei.Savu"
>> >> > "Bruno.Dumon"
>> >> > [5] "Edward.J..Yoon"       "Eugene.Koontz"        "Jakob.Homan"
>> >> > "Kelvin.Kakugawa"
>> >> > [9] "Kirk.True"            "Lars.George"         
>"Soren.Macbeth"
>> >> > "Stu.Hood"
>> >> >[13] "Tibor.Kiss"           "Tom.White"            "Unassigned"
>> >> >"David.Alves"
>> >> >[17] "Patrick.Hunt"         "Sebastian.Schoenherr" "Roman.Valls"
>> >> > "Pavel.Yaskevich"
>> >> >[21] "Karel.Vervaeke"       "Shay.Banon"           "Hyunsik.Choi"
>> >> >"Joey.Hung."
>> >> >[25] "Tommaso.Teofili"      "Jeff.Hammerbacher."  
>"Andrew.Purtell."
>> >> > "Johan.Oskarsson"
>> >> >[29] "Erik.Frey"            "Sebb"                
>"Andrew.klochkov"
>> >> > "Paul.Egan"
>> >> >[33] "Hudson"               "Hadoop"               "Aaron"
>> >> > "henry"
>> >> >[37] "steve"                "russel"
>> >> >
>> >> >       [[alternative HTML version deleted]]
>> >> >
>> >> >______________________________________________
>> >> >R-help at r-project.org mailing list
>> >> >https://stat.ethz.ch/mailman/listinfo/r-help
>> >> >PLEASE do read the posting guide
>> >> >http://www.R-project.org/posting-guide.html
>> >> >and provide commented, minimal, self-contained, reproducible
>code.
>> >>
>> >>
>>
>>




More information about the R-help mailing list