[R] how to read this kind of csv in R?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sun Oct 6 14:25:26 CEST 2019


On 06/10/2019 8:23 a.m., vod vos wrote:
> The problem is aa, bb and cc all in a single csv file
> contains no blank line.

So what steps do you need, and which of them do you need help with?

Duncan Murdoch

> The single csv file like list output.
> 
> aa(cm)
>   1, 2 , 3,
>   bb(mm)
>    1, 2, 3,
>   4, 5, 6,
>   7, 8, 9,
>   cc(mm)
>   3, 4, 5,
>   7, 5, 9,
>   6, 5, 8,
> 
> 
> 
>   ---- 在 星期日, 06 十月 2019 05:08:41 -0700 Duncan Murdoch <murdoch.duncan using gmail.com> 撰写 ----
>   > On 06/10/2019 7:29 a.m., vod vos via R-help wrote:
>   > > I got hundreds of csv files. The real formats in each csv file are as follows:
>   > >
>   > > aa(cm)
>   > > 1, 2 , 3,
>   > >
>   > > bb(mm)
>   > > 1, 2, 3,
>   > > 4, 5, 6,
>   > > 7, 8, 9,
>   > >
>   > > cc(mm)
>   > > 3, 4, 5,
>   > > 7, 5, 9,
>   > > 6, 5, 8,
>   > >
>   > > How can I use read.table or read.csv to convert the csv files
>   > > to a tidy data frame format as follow:
>   > >
>   > > aa, bb, cc
>   > > 1, 1, 3
>   > > 1, 2, 4
>   > > 1, 3, 5
>   > > 2, 4, 7
>   > > 2, 5, 5
>   > > 2, 6, 9
>   > > 3, 7, 6
>   > > 3, 8, 5
>   > > 3, 9, 8
>   > >
>   > > many thanks.
>   >
>   > You'll need more than those two functions to do the transformation you
>   > want.  To work out what you need, write out the process in detail in
>   > English (or another natural language), not in code.  For example:
>   >
>   > 1.  Read aa from file 1.
>   > 2.  Read bb from file 2.
>   > 3.  Read cc from file 3.
>   > 4.  Expand all vectors to the same length.
>   > 5.  Combine them into a single dataframe.
>   >
>   > Then work out each step separately.  I think you'll want to use
>   > something like scan("filename", skip = 1, sep = ",") in steps 1, 2, and
>   > 3, but this will add NA values at the end of each line because of the
>   > final comma, so you could do this:
>   >
>   > aa <- scan("file1", skip = 1, sep = ",")
>   > aa <- aa[!is.na(aa)]
>   >
>   > and similarly for the others.
>   >
>   > I don't know the rules for expanding that you'll need in your real data,
>   > but for your example step 4 could be
>   >
>   >    aa <- rep(aa, each = 3)
>   >
>   > Then step 5 could be
>   >
>   >    result <- data.frame(aa, bb, cc)
>   >
>   > Duncan Murdoch
>   >
>



More information about the R-help mailing list