Hello, I have a data set like one below.

 

First, 

I'd like to replace the empty cells with NA, and then the one immediately
above.

I could replace NAs with the immediate one, but don't know for the empty
cells.

 

index <- which(is.na(data1$year))

while (any(index)) {

  dummy$data1[index] <- dummy$data1[index - 1]

index <- which(is.na(data1$year))

}

 

Second,

The id in fact represents year-month-date of sampling (i.e., 10104 means
2001-01-04).

How can I split the id column into three columns of year, month, and date,
respectively such that I can do time-series analysis after aggregating them
for each month?

Or, Is there a way to transform the id into Date for doing irregular ts
analysis?

 

With much thanks,

Keun-Hyung

 

> data1

    year month    id

1   2001   Jan 10104

2     NA       10110

3     NA       10116

4     NA       10122

5     NA   Feb 10201

6     NA       10208

7     NA       10216

8     NA       10226

9     NA   Mar 10303

10    NA       10309

50  2002   Jan 20104

51    NA       20111

52    NA       20117

53    NA       20124

54    NA   Feb 20201

55    NA       20207

56    NA       20214

57    NA       20227

58    NA   Mar 20306

59    NA       20313

60    NA       20320

94  2003   Jan 30103

95    NA       30113

96    NA       30122

97    NA       30128

98    NA   Feb 30204

99    NA       30214

100   NA       30219

101   NA   Mar 30307

102   NA       30313

103   NA       30317

104   NA       30324

 

> str(data1)
'data.frame':     32 obs. of  3 variables:
 $ year : int  2001 NA NA NA NA NA NA NA NA NA ...
 $ month: chr  "Jan" "" "" "" ...
 $ id   : int  10104 10110 10116 10122 10201 10208 10216 10226 10303 10309
...

 

 

 


	[[alternative HTML version deleted]]

