数据整理和清洁

时间:2017-04-19 18:25:20

标签: r tidyr data-cleaning

我正在清理15个数据框的列表y(每个数据框由14个变量和不同的行数组成)。

所有数据框都与下面的y[[1]]

非常相似
      Time..CET.    Temp. Windchill Dew.Point Humidity Pressure Visibility Wind.Dir
1       12:00 AM   4.0 °C    0.3 °C   -1.0 °C      70% 1018 hPa          -      WSW
2       12:30 AM   3.0 °C   -1.0 °C   -1.0 °C      75% 1018 hPa          -      WSW
3        1:00 AM     3 °C         -     -1 °C      64% 1019 hPa      20 km       SW
4        1:00 AM   3.0 °C   -0.7 °C   -1.0 °C      75% 1018 hPa          -      WSW
5        1:30 AM   3.0 °C   -1.0 °C   -1.0 °C      75% 1018 hPa          -      WSW
6        2:00 AM   3.0 °C   -0.7 °C   -1.0 °C      75% 1018 hPa          -       SW
7        2:30 AM   2.0 °C   -1.9 °C   -1.0 °C      81% 1018 hPa          -       SW
8        3:00 AM   3.0 °C   -1.0 °C   -1.0 °C      75% 1018 hPa          -      WSW
9        3:30 AM   3.0 °C   -0.7 °C   -1.0 °C      75% 1018 hPa          -       SW
10       4:00 AM   2.0 °C   -2.2 °C   -1.0 °C      81% 1018 hPa          -       SW
11       4:30 AM   3.0 °C   -1.2 °C   -1.0 °C      75% 1018 hPa          -       SW
12       5:00 AM   3.0 °C   -1.0 °C   -1.0 °C      75% 1018 hPa          -      WSW
13       5:30 AM   2.0 °C   -1.9 °C   -1.0 °C      81% 1018 hPa          -       SW
14       6:00 AM   3.0 °C   -0.7 °C   -1.0 °C      75% 1018 hPa          -       SW
15       6:30 AM   3.0 °C   -0.7 °C    0.0 °C      81% 1018 hPa          -       SW
16       7:00 AM     3 °C         -     -1 °C      71% 1019 hPa      20 km      SSW
17       7:00 AM   3.0 °C   -1.0 °C    0.0 °C      81% 1018 hPa          -       SW
18       7:30 AM   3.0 °C   -0.3 °C    0.0 °C      81% 1018 hPa          -      SSW
19       8:00 AM   3.0 °C   -0.3 °C    0.0 °C      81% 1018 hPa          -       SW
20       8:30 AM   3.0 °C   -0.7 °C    0.0 °C      81% 1018 hPa    10.0 km       SW
21       9:00 AM   3.0 °C   -1.0 °C    0.0 °C      81% 1018 hPa    10.0 km       SW
22       9:30 AM   4.0 °C    0.3 °C    0.0 °C      75% 1018 hPa    10.0 km      SSW
23      10:00 AM     5 °C         -      0 °C      61% 1018 hPa      20 km       SW
24      10:00 AM   5.0 °C    1.5 °C    0.0 °C      70% 1018 hPa    10.0 km       SW
25      10:30 AM   6.0 °C    2.5 °C    1.0 °C      70% 1018 hPa          -       SW
26      11:00 AM   6.0 °C    2.5 °C    1.0 °C      70% 1017 hPa          -       SW
27      11:30 AM   7.0 °C    3.8 °C    1.0 °C      66% 1017 hPa    10.0 km       SW
28      12:00 PM   7.0 °C    3.6 °C    1.0 °C      66% 1017 hPa    10.0 km       SW
29      12:30 PM   7.0 °C    4.0 °C    1.0 °C      66% 1016 hPa    10.0 km       SW
30       1:00 PM     7 °C         -      1 °C      55% 1017 hPa      20 km       SW
31       1:00 PM   7.0 °C    3.8 °C    1.0 °C      66% 1016 hPa          -       SW
32       1:30 PM   7.0 °C    4.0 °C    1.0 °C      66% 1016 hPa    10.0 km       SW
33       2:00 PM   6.0 °C    2.8 °C    1.0 °C      70% 1016 hPa    10.0 km      SSW
34       2:30 PM   6.0 °C    3.0 °C    1.0 °C      70% 1016 hPa          -      SSW
35       3:00 PM   6.0 °C    3.3 °C    1.0 °C      70% 1015 hPa          -      SSW
36       3:30 PM   6.0 °C    3.0 °C    1.0 °C      70% 1015 hPa          -       SW
37       4:00 PM     6 °C         -      0 °C      59% 1016 hPa      10 km      SSW
38       4:00 PM   5.0 °C    1.5 °C    1.0 °C      76% 1015 hPa          -       SW
39       4:30 PM   5.0 °C    1.5 °C    1.0 °C      76% 1016 hPa          -       SW
40       5:00 PM   5.0 °C    1.5 °C    1.0 °C      76% 1015 hPa          -      SSW
41       5:30 PM   5.0 °C    1.0 °C    0.0 °C      70% 1015 hPa    10.0 km      SSW
42       6:00 PM   5.0 °C    1.5 °C    1.0 °C      76% 1015 hPa    10.0 km       SW
43       6:30 PM   5.0 °C    1.5 °C    1.0 °C      76% 1015 hPa          -       SW
44       7:00 PM     5 °C         -     -0 °C      63% 1016 hPa      20 km       SW
45       7:00 PM   5.0 °C    1.8 °C    0.0 °C      70% 1015 hPa          -       SW
46       7:30 PM   5.0 °C    1.8 °C    0.0 °C      70% 1015 hPa    10.0 km       SW
47       8:00 PM   4.0 °C    0.9 °C    0.0 °C      75% 1015 hPa    10.0 km       SW
48       8:30 PM   5.0 °C    2.1 °C    0.0 °C      70% 1015 hPa    10.0 km      SSW
49       9:00 PM   4.0 °C    1.2 °C    0.0 °C      75% 1014 hPa          -      SSW
50       9:30 PM   3.0 °C   -0.3 °C    0.0 °C      81% 1014 hPa          -      SSW
51      10:00 PM     2 °C         -     -1 °C      73% 1015 hPa      20 km    South
52      10:00 PM   2.0 °C   -1.2 °C   -1.0 °C      81% 1014 hPa          -      SSW
53      10:30 PM   2.0 °C   -1.2 °C   -1.0 °C      81% 1014 hPa          -    South
54      11:00 PM   2.0 °C   -1.2 °C   -1.0 °C      81% 1014 hPa          -    South
55      11:30 PM   2.0 °C   -1.2 °C   -1.0 °C      81% 1014 hPa          -    South
56      12:30 AM   1.0 °C   -2.4 °C   -1.0 °C      87% 1013 hPa          -    South
57       1:00 AM     1 °C         -     -1 °C      79% 1014 hPa      20 km    South
58       1:00 AM   1.0 °C   -2.4 °C   -1.0 °C      87% 1013 hPa          -    South
59       1:30 AM   2.0 °C   -1.2 °C   -1.0 °C      81% 1013 hPa          -    South
60       2:00 AM   2.0 °C   -1.5 °C   -1.0 °C      81% 1012 hPa          -    South
61       2:30 AM   2.0 °C   -1.9 °C   -1.0 °C      81% 1012 hPa          -    South
62       3:00 AM   2.0 °C   -1.5 °C   -1.0 °C      81% 1012 hPa          -    South
63       3:30 AM   1.0 °C   -2.8 °C   -2.0 °C      81% 1011 hPa          -    South
64       4:00 AM     1 °C         -     -2 °C      72% 1012 hPa      20 km    South
65       4:00 AM   1.0 °C   -3.1 °C   -2.0 °C      81% 1011 hPa          -    South
66       4:30 AM   1.0 °C   -3.4 °C   -2.0 °C      81% 1011 hPa          -    South
67       5:00 AM   2.0 °C   -1.9 °C   -2.0 °C      75% 1010 hPa          -    South
68       5:30 AM   0.0 °C   -3.6 °C   -3.0 °C      80% 1010 hPa          -      SSE
69       6:00 AM   0.0 °C   -4.4 °C   -2.0 °C      87% 1010 hPa          -    South
70       6:30 AM   0.0 °C   -3.6 °C   -2.0 °C      87% 1010 hPa          -    South
71       7:00 AM     0 °C         -     -3 °C      74% 1011 hPa      20 km      SSE
                      Wind.Speed                Gust.Speed Precip                             Events
1       16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
2       16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
3             10.8 km/h\n\t\t/                           -      -                               <NA>
4       14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
5       16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
6       14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
7       14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
8       16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
9       14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
10      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
11      18.5 km/h\n\t\t/ 5.1 m/s                         -    N/A                               <NA>
12      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
13      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
14      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
15      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
16            10.8 km/h\n\t\t/                           -      -                               <NA>
17      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
18      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
19      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
20      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
21      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
22      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
23            14.4 km/h\n\t\t/                           -      -                               <NA>
24      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
25      18.5 km/h\n\t\t/ 5.1 m/s                         -    N/A                               <NA>
26      18.5 km/h\n\t\t/ 5.1 m/s                         -    N/A                               <NA>
27      18.5 km/h\n\t\t/ 5.1 m/s                         -    N/A                               <NA>
28      20.4 km/h\n\t\t/ 5.7 m/s                         -    N/A                               <NA>
29      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
30            14.4 km/h\n\t\t/                           -      -                               <NA>
31      18.5 km/h\n\t\t/ 5.1 m/s                         -    N/A                               <NA>
32      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
33      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
34      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
35      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
36      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
37            10.8 km/h\n\t\t/                           -      -                               <NA>
38      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
39      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
40      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
41      20.4 km/h\n\t\t/ 5.7 m/s                         -    N/A                               <NA>
42      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
43      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
44            10.8 km/h\n\t\t/                           -      -                               <NA>
45      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
46      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
47      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
48      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
49      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
50      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
51             3.6 km/h\n\t\t/                           -      -                               <NA>
52      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
53      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
54      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
55      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
56      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
57             7.2 km/h\n\t\t/                           -      -                               <NA>
58      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
59      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
60      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
61      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
62      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
63      13.0 km/h\n\t\t/ 3.6 m/s                         -    N/A                               <NA>
64             7.2 km/h\n\t\t/                           -      -                               <NA>
65      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
66      16.7 km/h\n\t\t/ 4.6 m/s                         -    N/A                               <NA>
67      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
68      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
69      14.8 km/h\n\t\t/ 4.1 m/s                         -    N/A                               <NA>
70      11.1 km/h\n\t\t/ 3.1 m/s                         -    N/A                               <NA>
71             7.2 km/h\n\t\t/                           -      -                               <NA>
                        Conditions                                   Date.Place
1                            Clear     Warsaw, Poland Monday, December 31, 2012
2                            Clear     Warsaw, Poland Monday, December 31, 2012
3                 Scattered Clouds     Warsaw, Poland Monday, December 31, 2012
4                            Clear     Warsaw, Poland Monday, December 31, 2012
5                            Clear     Warsaw, Poland Monday, December 31, 2012
6                            Clear     Warsaw, Poland Monday, December 31, 2012
7                            Clear     Warsaw, Poland Monday, December 31, 2012
8                            Clear     Warsaw, Poland Monday, December 31, 2012
9                            Clear     Warsaw, Poland Monday, December 31, 2012
10                           Clear     Warsaw, Poland Monday, December 31, 2012
11                           Clear     Warsaw, Poland Monday, December 31, 2012
12                           Clear     Warsaw, Poland Monday, December 31, 2012
13                           Clear     Warsaw, Poland Monday, December 31, 2012
14                           Clear     Warsaw, Poland Monday, December 31, 2012
15                           Clear     Warsaw, Poland Monday, December 31, 2012
16                   Mostly Cloudy     Warsaw, Poland Monday, December 31, 2012
17                           Clear     Warsaw, Poland Monday, December 31, 2012
18                           Clear     Warsaw, Poland Monday, December 31, 2012
19                           Clear     Warsaw, Poland Monday, December 31, 2012
20                   Mostly Cloudy     Warsaw, Poland Monday, December 31, 2012
21                   Mostly Cloudy     Warsaw, Poland Monday, December 31, 2012
22                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
23                Scattered Clouds     Warsaw, Poland Monday, December 31, 2012
24                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
25                           Clear     Warsaw, Poland Monday, December 31, 2012
26                           Clear     Warsaw, Poland Monday, December 31, 2012
27                Scattered Clouds     Warsaw, Poland Monday, December 31, 2012
28                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
29                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
30                   Mostly Cloudy     Warsaw, Poland Monday, December 31, 2012
31                           Clear     Warsaw, Poland Monday, December 31, 2012
32                Scattered Clouds     Warsaw, Poland Monday, December 31, 2012
33                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
34                           Clear     Warsaw, Poland Monday, December 31, 2012
35                           Clear     Warsaw, Poland Monday, December 31, 2012
36                           Clear     Warsaw, Poland Monday, December 31, 2012
37                Scattered Clouds     Warsaw, Poland Monday, December 31, 2012
38                           Clear     Warsaw, Poland Monday, December 31, 2012
39                           Clear     Warsaw, Poland Monday, December 31, 2012
40                           Clear     Warsaw, Poland Monday, December 31, 2012
41                   Mostly Cloudy     Warsaw, Poland Monday, December 31, 2012
42                   Mostly Cloudy     Warsaw, Poland Monday, December 31, 2012
43                           Clear     Warsaw, Poland Monday, December 31, 2012
44                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
45                           Clear     Warsaw, Poland Monday, December 31, 2012
46                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
47                   Partly Cloudy     Warsaw, Poland Monday, December 31, 2012
48                Scattered Clouds     Warsaw, Poland Monday, December 31, 2012
49                           Clear     Warsaw, Poland Monday, December 31, 2012
50                           Clear     Warsaw, Poland Monday, December 31, 2012
51                Scattered Clouds     Warsaw, Poland Monday, December 31, 2012
52                           Clear     Warsaw, Poland Monday, December 31, 2012
53                           Clear     Warsaw, Poland Monday, December 31, 2012
54                           Clear     Warsaw, Poland Monday, December 31, 2012
55                           Clear     Warsaw, Poland Monday, December 31, 2012
56                           Clear      Warsaw, Poland Tuesday, January 1, 2013
57                   Partly Cloudy      Warsaw, Poland Tuesday, January 1, 2013
58                           Clear      Warsaw, Poland Tuesday, January 1, 2013
59                           Clear      Warsaw, Poland Tuesday, January 1, 2013
60                           Clear      Warsaw, Poland Tuesday, January 1, 2013
61                           Clear      Warsaw, Poland Tuesday, January 1, 2013
62                           Clear      Warsaw, Poland Tuesday, January 1, 2013
63                           Clear      Warsaw, Poland Tuesday, January 1, 2013
64                   Partly Cloudy      Warsaw, Poland Tuesday, January 1, 2013
65                           Clear      Warsaw, Poland Tuesday, January 1, 2013
66                           Clear      Warsaw, Poland Tuesday, January 1, 2013
67                           Clear      Warsaw, Poland Tuesday, January 1, 2013
68                           Clear      Warsaw, Poland Tuesday, January 1, 2013
69                           Clear      Warsaw, Poland Tuesday, January 1, 2013
70                           Clear      Warsaw, Poland Tuesday, January 1, 2013
71                           Clear      Warsaw, Poland Tuesday, January 1, 2013

在我的代码中,我将第一列Time..CET.分成两个TimeCET,过滤了整个小时的行,然后将最后一列分成两个新的DatePlace。其他一些清洁之后。 代码:

for(i in 1:length(y)){
  y[[i]] <- y[[i]] %>% 
  separate(col = "Time..CET.", into = c("Time", "CET"), sep =" ") %>%
  filter(grepl(':00', Time))

  v1<- as.character(y[[i]][,15]) %>% sapply(function(x) {strsplit(x, split=" ")})
  y[[i]][,15] <- NULL

    for(j in 1:length(v1)) {
    y[[i]][j,15] <- v1[[j]][4:6] %>% paste(collapse = "-") %>% as.Date(format = "%B-%d,-%Y")
    y[[i]][j,16] <- v1[[j]][1]
    }

  colnames(y[[i]])[15] <- "Date"
  colnames(y[[i]])[16] <- "Place"
  y[[i]] <- y[[i]] %>% sapply(function(x) {gsub("\\s+.*", "", x)})
  y[[i]] <- y[[i]] %>% sapply(function(x) {gsub("%", "", x)})

}

问题:我不知道为什么但是在循环内部的第10个元素(每次我尝试做计算时都会发生)y列表变成空的并且没有错误Error in v1[[j]] : subscript out of bounds

$ :'data.frame':    0 obs. of  14 variables:
  ..$ Time      : chr(0) 
  ..$ CET       : chr(0) 
  ..$ Temp.     : chr(0) 
  ..$ Windchill : chr(0) 
  ..$ Dew.Point : chr(0) 
  ..$ Humidity  : chr(0) 
  ..$ Pressure  : chr(0) 
  ..$ Visibility: chr(0) 
  ..$ Wind.Dir  : chr(0) 
  ..$ Wind.Speed: chr(0) 
  ..$ Gust.Speed: chr(0) 
  ..$ Precip    : chr(0) 
  ..$ Events    : chr(0) 
  ..$ Conditions: chr(0)

该错误可能是什么原因?为什么第10个元素变空? 也许有更顺畅的方法来整理这些数据?

0 个答案:

没有答案