[R] How to combine data columns to a single column

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 27 11:41:09 CEST 2010


On Wed, May 26, 2010 at 10:51 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> This is a time series so its best to represent it as such using a ts
> object.  Below tt is the vector of data you asked for and tt.ts is the
> same vector converted into a ts series:
>
> Lines <- "Year 1903 1904 1905 1906
> Jan 125.0 30.0 113.0 5.0
> Feb 128.0 100.0 70.0 388.0
> Mar 155.0 79.0 230.0 323.0
> Apr 199.0 89.0 104.0 199.0
> May 215.0 96.0 50.0 162.0
> Jun 228.0 16.0 41.0 154.0
> Jul 26.0 49.0 63.0 35.0
> Aug 19.0 118.0 55.0 180.0
> Sep 94.0 92.0 57.0 161.0
> Oct 150.0 47.0 42.0 142.0
> Nov 18.0 118.0 160.0 81.0
> Dec 128.0 234.0 344.0 76.0"
>
> # read into data frame
> DF <- read.table(textConnection(Lines), header = TRUE, check.names = FALSE)
> tt <- c(as.matrix(DF))

This last line should be:

tt <- c(as.matrix(DF[-1]))

as was pointed out offline.

>
> # create ts object tt.ts
> st <- as.numeric(names(DF)[2]) # 1903
> tt.ts <- ts(tt, freq = 12, start = st)
>
> plot(tt.ts)
>
> On Wed, May 26, 2010 at 10:30 PM, Zablone Owiti <zowiti at ncst.go.ke> wrote:
>> Dear users,
>>
>> I have several columns of data (each column containing monthly data for a
>> particular year from january - december) . I would wish  to combine  the
>> columns to get a Single column of continuous data as shown in (b) below. I
>> have read this data as table in R
>> (a) Data example
>>
>> Year     1903    1904    1905    1906    Jan     125.0   30.0    113.0   5.0     Feb
>>         128.0   100.0   70.0    388.0   Mar     155.0   79.0    230.0   323.0   Apr
>> 199.0    89.0    104.0   199.0   May     215.0   96.0    50.0    162.0   Jun
>> 228.0    16.0    41.0    154.0   Jul     26.0    49.0    63.0    35.0    Aug     19.0
>>         118.0   55.0    180.0   Sep     94.0    92.0    57.0    161.0   Oct     150.0
>> 47.0     42.0    142.0   Nov     18.0    118.0   160.0   81.0    Dec     128.0
>> 234.0    344.0   76.0
>>
>> (b)Single column :
>>
>> Jan      125.0   Feb     128.0   Mar     155.0   Apr     199.0   May     215.0   Jun
>>         228.0   Jul     26.0    Aug     19.0    Sep     94.0    Oct     150.0   Nov     18.0
>> Dec      128.0   Jan     30.0    Feb     100.0   Mar     79.0    Apr     89.0    May
>> 96.0     Jun     16.0    Jul     49.0    Aug     118.0   Sep     92.0    Oct     47.0
>> Nov      118.0   Dec     234.0   Jan     113.0   Feb     70.0    Mar     230.0   Apr
>>         104.0   May     50.0    Jun     41.0    Jul     63.0    Aug     55.0    Sep     57.0
>> Oct      42.0    Nov     160.0   Dec     344.0
>>
>> Jan      5.0     Feb     388.0   Mar     323.0   Apr     199.0   May     162.0   Jun
>> 154.0    Jul     35.0    Aug     180.0   Sep     161.0   Oct     142.0   Nov     81.0
>> Dec      76.0
>>
>> Thanks
>>
>> ----------------------------
>>  ZABLONE OWITI
>>  GRADUATE STUDENT
>> College of Atmospheric Science
>> Nanjing University of Information, Science and Technology
>> Add: 219 Ning Liu Rd, Nanjing, Jiangsu, 21004, P.R. China
>>  Tel: +86-25-58731402
>> Fax: +86-25-58731456
>> Mob. 15077895632
>> Website:  www.nuist.edu.cn
>> ====================================================
>>
>>
>>        [[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