Data Frame

nonhomogeneous, number of rows for each clomns are the same


v1 <- c("ahmet","mehmet","hüseyin")
v2 <- c( TRUE, FALSE, FALSE)
m1 <- matrix(1:6 , 3, 2)

df1 <- data.frame(v1,v2,m1)
df1
##        v1    v2 X1 X2
## 1   ahmet  TRUE  1  4
## 2  mehmet FALSE  2  5
## 3 hüseyin FALSE  3  6
df1[1]
df1[[1]]
str(df1)

df1 <- data.frame(v1,v2,m1,stringsAsFactors = F)
df1[1]
df1[[1]]
str(df1)
df1$v1

df1
##        v1    v2 X1 X2
## 1   ahmet  TRUE  1  4
## 2  mehmet FALSE  2  5
## 3 hüseyin FALSE  3  6
df1[1:4]
df1[1:4,3]
df1[1:6,3]
df1[3,1:4]
df1$v2
## [1]  TRUE FALSE FALSE
as.character(df1$v2)
df1$v2
df1$v2 <- as.character(df1$v2)
as.numeric(df1$v2)

attributes(df1)
## $names
## [1] "v1" "v2" "X1" "X2"
## 
## $class
## [1] "data.frame"
## 
## $row.names
## [1] 1 2 3
attributes(df1)$names
attr(df1,"names")

attr(df1,"names") <- c("isim","logic","sayi1","sayi2")
df1
attr(df1,"$row.names") <- c("a","b","c")
df1

read.csv("https://web.itu.edu.tr/~tokerem/18397_Cekmekoy_Omerli_15dk.txt")
##     sta_no.year.month.day.hour.minutes.temp.precipitation.pressure.relative_humidity
## 1                                                18397;2017;7;26;18;0;23.9;0;1003;94
## 2                                             18397;2017;7;26;18;15;23.9;0;1003.1;95
## 3                                             18397;2017;7;26;18;30;23.8;0;1003.2;96
## 4                                             18397;2017;7;26;18;45;23.8;0;1003.2;96
## 5                                              18397;2017;7;26;19;0;23.6;0;1003.2;96
## 6                                             18397;2017;7;26;19;15;23.2;0;1003.1;97
## 7                                             18397;2017;7;26;19;30;23.2;0;1003.1;97
## 8                                             18397;2017;7;26;19;45;23.1;0;1003.1;98
## 9                                                18397;2017;7;26;20;0;23;0;1003.1;98
## 10                                              18397;2017;7;26;20;15;22.8;0;1003;98
## 11                                              18397;2017;7;26;20;30;22.5;0;1003;98
## 12                                              18397;2017;7;26;20;45;22.4;0;1003;99
## 13                                               18397;2017;7;26;21;0;22.2;0;1003;99
## 14                                              18397;2017;7;26;21;15;22.3;0;1003;99
## 15                                            18397;2017;7;26;21;30;22.2;0;1003.1;99
## 16                                            18397;2017;7;26;21;45;21.7;0;1003.1;99
## 17                                             18397;2017;7;26;22;0;21.9;0;1003.2;99
## 18                                            18397;2017;7;26;22;15;21.7;0;1003.3;99
## 19                                            18397;2017;7;26;22;30;21.6;0;1003.3;99
## 20                                           18397;2017;7;26;22;45;22.2;0;1003.4;100
## 21                                            18397;2017;7;26;23;0;22.2;0;1003.4;100
## 22                                           18397;2017;7;26;23;15;22.1;0;1003.5;100
## 23                                           18397;2017;7;26;23;30;22.3;0;1003.4;100
## 24                                           18397;2017;7;26;23;45;22.5;0;1003.4;100
## 25                                             18397;2017;7;27;0;0;22.3;0;1003.4;100
## 26                                            18397;2017;7;27;0;15;22.2;0;1003.2;100
## 27                                            18397;2017;7;27;0;30;22.5;0;1003.2;100
## 28                                            18397;2017;7;27;0;45;22.6;0;1003.2;100
## 29                                             18397;2017;7;27;1;0;22.6;0;1003.3;100
## 30                                            18397;2017;7;27;1;15;22.6;0;1003.4;100
## 31                                            18397;2017;7;27;1;30;22.6;0;1003.2;100
## 32                                            18397;2017;7;27;1;45;22.7;0;1003.2;100
## 33                                             18397;2017;7;27;2;0;22.6;0;1003.3;100
## 34                                            18397;2017;7;27;2;15;22.5;0;1003.2;100
## 35                                            18397;2017;7;27;2;30;22.6;0;1003.2;100
## 36                                            18397;2017;7;27;2;45;22.5;0;1003.1;100
## 37                                             18397;2017;7;27;3;0;22.5;0;1003.1;100
## 38                                              18397;2017;7;27;3;15;22.4;0;1003;100
## 39                                            18397;2017;7;27;3;30;22.5;0;1003.1;100
## 40                                            18397;2017;7;27;3;45;22.4;0;1003.3;100
## 41                                             18397;2017;7;27;4;0;22.5;0;1003.4;100
## 42                                            18397;2017;7;27;4;15;22.6;0;1003.5;100
## 43                                              18397;2017;7;27;4;30;23;0;1003.5;100
## 44                                            18397;2017;7;27;4;45;23.2;0;1003.5;100
## 45                                             18397;2017;7;27;5;0;24.2;0;1003.6;100
## 46                                             18397;2017;7;27;5;15;25.1;0;1003.5;97
## 47                                             18397;2017;7;27;5;30;25.5;0;1003.4;84
## 48                                             18397;2017;7;27;5;45;26.1;0;1003.3;82
## 49                                              18397;2017;7;27;6;0;27.1;0;1003.3;79
## 50                                             18397;2017;7;27;6;15;26.9;0;1003.3;78
## 51                                             18397;2017;7;27;6;30;27.6;0;1003.3;78
## 52                                               18397;2017;7;27;6;45;28;0;1003.2;76
## 53                                              18397;2017;7;27;7;0;28.4;0;1003.1;76
## 54                                             18397;2017;7;27;7;15;28.5;0;1003.1;75
## 55                                               18397;2017;7;27;7;30;29.3;0;1003;73
## 56                                             18397;2017;7;27;7;45;30.2;0;1002.9;65
## 57                                              18397;2017;7;27;8;0;30.1;0;1002.8;57
## 58                                             18397;2017;7;27;8;15;30.1;0;1002.8;60
## 59                                             18397;2017;7;27;8;30;30.4;0;1002.8;53
## 60                                             18397;2017;7;27;8;45;30.4;0;1002.8;52
## 61                                              18397;2017;7;27;9;0;30.8;0;1002.9;51
## 62                                             18397;2017;7;27;9;15;30.9;0;1002.8;51
## 63                                               18397;2017;7;27;9;30;31;0;1002.6;50
## 64                                             18397;2017;7;27;9;45;31.5;0;1002.6;53
## 65                                             18397;2017;7;27;10;0;31.2;0;1002.6;52
## 66                                            18397;2017;7;27;10;15;30.9;0;1002.4;57
## 67                                            18397;2017;7;27;10;30;30.9;0;1002.4;58
## 68                                            18397;2017;7;27;10;45;30.4;0;1002.3;59
## 69                                             18397;2017;7;27;11;0;30.4;0;1002.1;60
## 70                                              18397;2017;7;27;11;15;30;0;1001.9;61
## 71                                            18397;2017;7;27;11;30;29.2;0;1001.9;65
## 72                                            18397;2017;7;27;11;45;29.5;0;1001.7;66
## 73                                             18397;2017;7;27;12;0;29.4;0;1001.6;67
## 74                                            18397;2017;7;27;12;15;29.3;0;1001.3;66
## 75                                            18397;2017;7;27;12;30;29.6;0;1001.2;68
## 76                                            18397;2017;7;27;12;45;28.8;0;1001.3;70
## 77                                               18397;2017;7;27;13;0;29;0;1001.1;68
## 78                                              18397;2017;7;27;13;15;29;0;1001.2;69
## 79                                            18397;2017;7;27;13;30;29.2;0;1001.3;69
## 80                                            18397;2017;7;27;13;45;28.4;0;1001.5;71
## 81                                             18397;2017;7;27;14;0;27.8;0;1001.6;72
## 82                                            18397;2017;7;27;14;15;27.4;0;1001.6;72
## 83                                            18397;2017;7;27;14;30;26.6;0;1001.5;77
## 84                                            18397;2017;7;27;14;45;26.2;0;1001.2;79
## 85                                             18397;2017;7;27;15;0;25.8;0;1001.1;80
## 86                                              18397;2017;7;27;15;15;25.6;0;1001;82
## 87                                            18397;2017;7;27;15;30;25.4;0;1000.9;84
## 88                                            18397;2017;7;27;15;45;24.2;0;1001.8;79
## 89                                          18397;2017;7;27;16;0;19.2;7.01;1003.7;99
## 90                                         18397;2017;7;27;16;15;19.5;8.8;1003.2;100
## 91                                        18397;2017;7;27;16;30;20.1;0.25;1003.1;100
## 92                                           18397;2017;7;27;16;45;20.8;0;1003.7;100
## 93                                          18397;2017;7;27;17;0;21.2;1.13;-9999;100
## 94                                        18397;2017;7;27;17;15;21.4;0.02;1005.6;100
## 95                                        18397;2017;7;27;17;30;21.4;1.25;1005.4;100
## 96                                        18397;2017;7;27;17;45;21.4;2.75;1005.1;100
## 97                                            18397;2017;7;27;18;0;21.2;0;1005.1;100
## 98                                              18397;2017;7;27;18;15;21;0;-9999;100
## 99                                           18397;2017;7;27;18;30;20.8;0;1006.3;100
## 100                                           18397;2017;7;27;18;45;20.9;0;-9999;100
## 101                                        18397;2017;7;27;19;0;20.8;0.19;1005.7;100
## 102                                          18397;2017;7;27;19;15;20.7;0;1006.2;100
## 103                                        18397;2017;7;27;19;30;20.8;0.2;1003.6;100
## 104                                       18397;2017;7;27;19;45;20.8;0.22;1003.7;100
## 105                                            18397;2017;7;27;20;0;20.9;0;-9999;100
## 106                                           18397;2017;7;27;20;15;20.6;0;-9999;100
## 107                                          18397;2017;7;27;20;30;20.6;0;1005.1;100
## 108                                          18397;2017;7;27;20;45;20.5;0;1005.6;100
## 109                                           18397;2017;7;27;21;0;20.7;0;1005.5;100
## 110                                          18397;2017;7;27;21;15;20.8;0;1005.7;100
## 111                                          18397;2017;7;27;21;30;20.4;0;1005.6;100
## 112                                          18397;2017;7;27;21;45;20.4;0;1005.8;100
## 113                                           18397;2017;7;27;22;0;20.6;0;1005.8;100
## 114                                          18397;2017;7;27;22;15;20.5;0;1005.9;100
## 115                                            18397;2017;7;27;22;30;20.4;0;1006;100
## 116                                          18397;2017;7;27;22;45;20.5;0;1005.9;100
## 117                                           18397;2017;7;27;23;0;20.5;0;1005.9;100
## 118                                          18397;2017;7;27;23;15;20.6;0;1005.9;100
## 119                                            18397;2017;7;27;23;30;20.5;0;1006;100
## 120                                            18397;2017;7;27;23;45;20.5;0;1006;100
## 121                                              18397;2017;7;28;0;0;20.4;0;1006;100

read.csv("https://web.itu.edu.tr/~tokerem/18397_Cekmekoy_Omerli_15dk.txt", sep = ";")
##     sta_no year month day hour minutes temp precipitation pressure
## 1    18397 2017     7  26   18       0 23.9          0.00   1003.0
## 2    18397 2017     7  26   18      15 23.9          0.00   1003.1
## 3    18397 2017     7  26   18      30 23.8          0.00   1003.2
## 4    18397 2017     7  26   18      45 23.8          0.00   1003.2
## 5    18397 2017     7  26   19       0 23.6          0.00   1003.2
## 6    18397 2017     7  26   19      15 23.2          0.00   1003.1
## 7    18397 2017     7  26   19      30 23.2          0.00   1003.1
## 8    18397 2017     7  26   19      45 23.1          0.00   1003.1
## 9    18397 2017     7  26   20       0 23.0          0.00   1003.1
## 10   18397 2017     7  26   20      15 22.8          0.00   1003.0
## 11   18397 2017     7  26   20      30 22.5          0.00   1003.0
## 12   18397 2017     7  26   20      45 22.4          0.00   1003.0
## 13   18397 2017     7  26   21       0 22.2          0.00   1003.0
## 14   18397 2017     7  26   21      15 22.3          0.00   1003.0
## 15   18397 2017     7  26   21      30 22.2          0.00   1003.1
## 16   18397 2017     7  26   21      45 21.7          0.00   1003.1
## 17   18397 2017     7  26   22       0 21.9          0.00   1003.2
## 18   18397 2017     7  26   22      15 21.7          0.00   1003.3
## 19   18397 2017     7  26   22      30 21.6          0.00   1003.3
## 20   18397 2017     7  26   22      45 22.2          0.00   1003.4
## 21   18397 2017     7  26   23       0 22.2          0.00   1003.4
## 22   18397 2017     7  26   23      15 22.1          0.00   1003.5
## 23   18397 2017     7  26   23      30 22.3          0.00   1003.4
## 24   18397 2017     7  26   23      45 22.5          0.00   1003.4
## 25   18397 2017     7  27    0       0 22.3          0.00   1003.4
## 26   18397 2017     7  27    0      15 22.2          0.00   1003.2
## 27   18397 2017     7  27    0      30 22.5          0.00   1003.2
## 28   18397 2017     7  27    0      45 22.6          0.00   1003.2
## 29   18397 2017     7  27    1       0 22.6          0.00   1003.3
## 30   18397 2017     7  27    1      15 22.6          0.00   1003.4
## 31   18397 2017     7  27    1      30 22.6          0.00   1003.2
## 32   18397 2017     7  27    1      45 22.7          0.00   1003.2
## 33   18397 2017     7  27    2       0 22.6          0.00   1003.3
## 34   18397 2017     7  27    2      15 22.5          0.00   1003.2
## 35   18397 2017     7  27    2      30 22.6          0.00   1003.2
## 36   18397 2017     7  27    2      45 22.5          0.00   1003.1
## 37   18397 2017     7  27    3       0 22.5          0.00   1003.1
## 38   18397 2017     7  27    3      15 22.4          0.00   1003.0
## 39   18397 2017     7  27    3      30 22.5          0.00   1003.1
## 40   18397 2017     7  27    3      45 22.4          0.00   1003.3
## 41   18397 2017     7  27    4       0 22.5          0.00   1003.4
## 42   18397 2017     7  27    4      15 22.6          0.00   1003.5
## 43   18397 2017     7  27    4      30 23.0          0.00   1003.5
## 44   18397 2017     7  27    4      45 23.2          0.00   1003.5
## 45   18397 2017     7  27    5       0 24.2          0.00   1003.6
## 46   18397 2017     7  27    5      15 25.1          0.00   1003.5
## 47   18397 2017     7  27    5      30 25.5          0.00   1003.4
## 48   18397 2017     7  27    5      45 26.1          0.00   1003.3
## 49   18397 2017     7  27    6       0 27.1          0.00   1003.3
## 50   18397 2017     7  27    6      15 26.9          0.00   1003.3
## 51   18397 2017     7  27    6      30 27.6          0.00   1003.3
## 52   18397 2017     7  27    6      45 28.0          0.00   1003.2
## 53   18397 2017     7  27    7       0 28.4          0.00   1003.1
## 54   18397 2017     7  27    7      15 28.5          0.00   1003.1
## 55   18397 2017     7  27    7      30 29.3          0.00   1003.0
## 56   18397 2017     7  27    7      45 30.2          0.00   1002.9
## 57   18397 2017     7  27    8       0 30.1          0.00   1002.8
## 58   18397 2017     7  27    8      15 30.1          0.00   1002.8
## 59   18397 2017     7  27    8      30 30.4          0.00   1002.8
## 60   18397 2017     7  27    8      45 30.4          0.00   1002.8
## 61   18397 2017     7  27    9       0 30.8          0.00   1002.9
## 62   18397 2017     7  27    9      15 30.9          0.00   1002.8
## 63   18397 2017     7  27    9      30 31.0          0.00   1002.6
## 64   18397 2017     7  27    9      45 31.5          0.00   1002.6
## 65   18397 2017     7  27   10       0 31.2          0.00   1002.6
## 66   18397 2017     7  27   10      15 30.9          0.00   1002.4
## 67   18397 2017     7  27   10      30 30.9          0.00   1002.4
## 68   18397 2017     7  27   10      45 30.4          0.00   1002.3
## 69   18397 2017     7  27   11       0 30.4          0.00   1002.1
## 70   18397 2017     7  27   11      15 30.0          0.00   1001.9
## 71   18397 2017     7  27   11      30 29.2          0.00   1001.9
## 72   18397 2017     7  27   11      45 29.5          0.00   1001.7
## 73   18397 2017     7  27   12       0 29.4          0.00   1001.6
## 74   18397 2017     7  27   12      15 29.3          0.00   1001.3
## 75   18397 2017     7  27   12      30 29.6          0.00   1001.2
## 76   18397 2017     7  27   12      45 28.8          0.00   1001.3
## 77   18397 2017     7  27   13       0 29.0          0.00   1001.1
## 78   18397 2017     7  27   13      15 29.0          0.00   1001.2
## 79   18397 2017     7  27   13      30 29.2          0.00   1001.3
## 80   18397 2017     7  27   13      45 28.4          0.00   1001.5
## 81   18397 2017     7  27   14       0 27.8          0.00   1001.6
## 82   18397 2017     7  27   14      15 27.4          0.00   1001.6
## 83   18397 2017     7  27   14      30 26.6          0.00   1001.5
## 84   18397 2017     7  27   14      45 26.2          0.00   1001.2
## 85   18397 2017     7  27   15       0 25.8          0.00   1001.1
## 86   18397 2017     7  27   15      15 25.6          0.00   1001.0
## 87   18397 2017     7  27   15      30 25.4          0.00   1000.9
## 88   18397 2017     7  27   15      45 24.2          0.00   1001.8
## 89   18397 2017     7  27   16       0 19.2          7.01   1003.7
## 90   18397 2017     7  27   16      15 19.5          8.80   1003.2
## 91   18397 2017     7  27   16      30 20.1          0.25   1003.1
## 92   18397 2017     7  27   16      45 20.8          0.00   1003.7
## 93   18397 2017     7  27   17       0 21.2          1.13  -9999.0
## 94   18397 2017     7  27   17      15 21.4          0.02   1005.6
## 95   18397 2017     7  27   17      30 21.4          1.25   1005.4
## 96   18397 2017     7  27   17      45 21.4          2.75   1005.1
## 97   18397 2017     7  27   18       0 21.2          0.00   1005.1
## 98   18397 2017     7  27   18      15 21.0          0.00  -9999.0
## 99   18397 2017     7  27   18      30 20.8          0.00   1006.3
## 100  18397 2017     7  27   18      45 20.9          0.00  -9999.0
## 101  18397 2017     7  27   19       0 20.8          0.19   1005.7
## 102  18397 2017     7  27   19      15 20.7          0.00   1006.2
## 103  18397 2017     7  27   19      30 20.8          0.20   1003.6
## 104  18397 2017     7  27   19      45 20.8          0.22   1003.7
## 105  18397 2017     7  27   20       0 20.9          0.00  -9999.0
## 106  18397 2017     7  27   20      15 20.6          0.00  -9999.0
## 107  18397 2017     7  27   20      30 20.6          0.00   1005.1
## 108  18397 2017     7  27   20      45 20.5          0.00   1005.6
## 109  18397 2017     7  27   21       0 20.7          0.00   1005.5
## 110  18397 2017     7  27   21      15 20.8          0.00   1005.7
## 111  18397 2017     7  27   21      30 20.4          0.00   1005.6
## 112  18397 2017     7  27   21      45 20.4          0.00   1005.8
## 113  18397 2017     7  27   22       0 20.6          0.00   1005.8
## 114  18397 2017     7  27   22      15 20.5          0.00   1005.9
## 115  18397 2017     7  27   22      30 20.4          0.00   1006.0
## 116  18397 2017     7  27   22      45 20.5          0.00   1005.9
## 117  18397 2017     7  27   23       0 20.5          0.00   1005.9
## 118  18397 2017     7  27   23      15 20.6          0.00   1005.9
## 119  18397 2017     7  27   23      30 20.5          0.00   1006.0
## 120  18397 2017     7  27   23      45 20.5          0.00   1006.0
## 121  18397 2017     7  28    0       0 20.4          0.00   1006.0
##     relative_humidity
## 1                  94
## 2                  95
## 3                  96
## 4                  96
## 5                  96
## 6                  97
## 7                  97
## 8                  98
## 9                  98
## 10                 98
## 11                 98
## 12                 99
## 13                 99
## 14                 99
## 15                 99
## 16                 99
## 17                 99
## 18                 99
## 19                 99
## 20                100
## 21                100
## 22                100
## 23                100
## 24                100
## 25                100
## 26                100
## 27                100
## 28                100
## 29                100
## 30                100
## 31                100
## 32                100
## 33                100
## 34                100
## 35                100
## 36                100
## 37                100
## 38                100
## 39                100
## 40                100
## 41                100
## 42                100
## 43                100
## 44                100
## 45                100
## 46                 97
## 47                 84
## 48                 82
## 49                 79
## 50                 78
## 51                 78
## 52                 76
## 53                 76
## 54                 75
## 55                 73
## 56                 65
## 57                 57
## 58                 60
## 59                 53
## 60                 52
## 61                 51
## 62                 51
## 63                 50
## 64                 53
## 65                 52
## 66                 57
## 67                 58
## 68                 59
## 69                 60
## 70                 61
## 71                 65
## 72                 66
## 73                 67
## 74                 66
## 75                 68
## 76                 70
## 77                 68
## 78                 69
## 79                 69
## 80                 71
## 81                 72
## 82                 72
## 83                 77
## 84                 79
## 85                 80
## 86                 82
## 87                 84
## 88                 79
## 89                 99
## 90                100
## 91                100
## 92                100
## 93                100
## 94                100
## 95                100
## 96                100
## 97                100
## 98                100
## 99                100
## 100               100
## 101               100
## 102               100
## 103               100
## 104               100
## 105               100
## 106               100
## 107               100
## 108               100
## 109               100
## 110               100
## 111               100
## 112               100
## 113               100
## 114               100
## 115               100
## 116               100
## 117               100
## 118               100
## 119               100
## 120               100
## 121               100
sta1 <- read.csv("https://web.itu.edu.tr/~tokerem/18397_Cekmekoy_Omerli_15dk.txt", sep = ";")

str(sta1)
## 'data.frame':    121 obs. of  10 variables:
##  $ sta_no           : int  18397 18397 18397 18397 18397 18397 18397 18397 18397 18397 ...
##  $ year             : int  2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
##  $ month            : int  7 7 7 7 7 7 7 7 7 7 ...
##  $ day              : int  26 26 26 26 26 26 26 26 26 26 ...
##  $ hour             : int  18 18 18 18 19 19 19 19 20 20 ...
##  $ minutes          : int  0 15 30 45 0 15 30 45 0 15 ...
##  $ temp             : num  23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23 22.8 ...
##  $ precipitation    : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ pressure         : num  1003 1003 1003 1003 1003 ...
##  $ relative_humidity: int  94 95 96 96 96 97 97 98 98 98 ...

attributes(sta1)
## $names
##  [1] "sta_no"            "year"              "month"            
##  [4] "day"               "hour"              "minutes"          
##  [7] "temp"              "precipitation"     "pressure"         
## [10] "relative_humidity"
## 
## $class
## [1] "data.frame"
## 
## $row.names
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102
## [103] 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
## [120] 120 121

sta1[25:121,2:10]
##     year month day hour minutes temp precipitation pressure
## 25  2017     7  27    0       0 22.3          0.00   1003.4
## 26  2017     7  27    0      15 22.2          0.00   1003.2
## 27  2017     7  27    0      30 22.5          0.00   1003.2
## 28  2017     7  27    0      45 22.6          0.00   1003.2
## 29  2017     7  27    1       0 22.6          0.00   1003.3
## 30  2017     7  27    1      15 22.6          0.00   1003.4
## 31  2017     7  27    1      30 22.6          0.00   1003.2
## 32  2017     7  27    1      45 22.7          0.00   1003.2
## 33  2017     7  27    2       0 22.6          0.00   1003.3
## 34  2017     7  27    2      15 22.5          0.00   1003.2
## 35  2017     7  27    2      30 22.6          0.00   1003.2
## 36  2017     7  27    2      45 22.5          0.00   1003.1
## 37  2017     7  27    3       0 22.5          0.00   1003.1
## 38  2017     7  27    3      15 22.4          0.00   1003.0
## 39  2017     7  27    3      30 22.5          0.00   1003.1
## 40  2017     7  27    3      45 22.4          0.00   1003.3
## 41  2017     7  27    4       0 22.5          0.00   1003.4
## 42  2017     7  27    4      15 22.6          0.00   1003.5
## 43  2017     7  27    4      30 23.0          0.00   1003.5
## 44  2017     7  27    4      45 23.2          0.00   1003.5
## 45  2017     7  27    5       0 24.2          0.00   1003.6
## 46  2017     7  27    5      15 25.1          0.00   1003.5
## 47  2017     7  27    5      30 25.5          0.00   1003.4
## 48  2017     7  27    5      45 26.1          0.00   1003.3
## 49  2017     7  27    6       0 27.1          0.00   1003.3
## 50  2017     7  27    6      15 26.9          0.00   1003.3
## 51  2017     7  27    6      30 27.6          0.00   1003.3
## 52  2017     7  27    6      45 28.0          0.00   1003.2
## 53  2017     7  27    7       0 28.4          0.00   1003.1
## 54  2017     7  27    7      15 28.5          0.00   1003.1
## 55  2017     7  27    7      30 29.3          0.00   1003.0
## 56  2017     7  27    7      45 30.2          0.00   1002.9
## 57  2017     7  27    8       0 30.1          0.00   1002.8
## 58  2017     7  27    8      15 30.1          0.00   1002.8
## 59  2017     7  27    8      30 30.4          0.00   1002.8
## 60  2017     7  27    8      45 30.4          0.00   1002.8
## 61  2017     7  27    9       0 30.8          0.00   1002.9
## 62  2017     7  27    9      15 30.9          0.00   1002.8
## 63  2017     7  27    9      30 31.0          0.00   1002.6
## 64  2017     7  27    9      45 31.5          0.00   1002.6
## 65  2017     7  27   10       0 31.2          0.00   1002.6
## 66  2017     7  27   10      15 30.9          0.00   1002.4
## 67  2017     7  27   10      30 30.9          0.00   1002.4
## 68  2017     7  27   10      45 30.4          0.00   1002.3
## 69  2017     7  27   11       0 30.4          0.00   1002.1
## 70  2017     7  27   11      15 30.0          0.00   1001.9
## 71  2017     7  27   11      30 29.2          0.00   1001.9
## 72  2017     7  27   11      45 29.5          0.00   1001.7
## 73  2017     7  27   12       0 29.4          0.00   1001.6
## 74  2017     7  27   12      15 29.3          0.00   1001.3
## 75  2017     7  27   12      30 29.6          0.00   1001.2
## 76  2017     7  27   12      45 28.8          0.00   1001.3
## 77  2017     7  27   13       0 29.0          0.00   1001.1
## 78  2017     7  27   13      15 29.0          0.00   1001.2
## 79  2017     7  27   13      30 29.2          0.00   1001.3
## 80  2017     7  27   13      45 28.4          0.00   1001.5
## 81  2017     7  27   14       0 27.8          0.00   1001.6
## 82  2017     7  27   14      15 27.4          0.00   1001.6
## 83  2017     7  27   14      30 26.6          0.00   1001.5
## 84  2017     7  27   14      45 26.2          0.00   1001.2
## 85  2017     7  27   15       0 25.8          0.00   1001.1
## 86  2017     7  27   15      15 25.6          0.00   1001.0
## 87  2017     7  27   15      30 25.4          0.00   1000.9
## 88  2017     7  27   15      45 24.2          0.00   1001.8
## 89  2017     7  27   16       0 19.2          7.01   1003.7
## 90  2017     7  27   16      15 19.5          8.80   1003.2
## 91  2017     7  27   16      30 20.1          0.25   1003.1
## 92  2017     7  27   16      45 20.8          0.00   1003.7
## 93  2017     7  27   17       0 21.2          1.13  -9999.0
## 94  2017     7  27   17      15 21.4          0.02   1005.6
## 95  2017     7  27   17      30 21.4          1.25   1005.4
## 96  2017     7  27   17      45 21.4          2.75   1005.1
## 97  2017     7  27   18       0 21.2          0.00   1005.1
## 98  2017     7  27   18      15 21.0          0.00  -9999.0
## 99  2017     7  27   18      30 20.8          0.00   1006.3
## 100 2017     7  27   18      45 20.9          0.00  -9999.0
## 101 2017     7  27   19       0 20.8          0.19   1005.7
## 102 2017     7  27   19      15 20.7          0.00   1006.2
## 103 2017     7  27   19      30 20.8          0.20   1003.6
## 104 2017     7  27   19      45 20.8          0.22   1003.7
## 105 2017     7  27   20       0 20.9          0.00  -9999.0
## 106 2017     7  27   20      15 20.6          0.00  -9999.0
## 107 2017     7  27   20      30 20.6          0.00   1005.1
## 108 2017     7  27   20      45 20.5          0.00   1005.6
## 109 2017     7  27   21       0 20.7          0.00   1005.5
## 110 2017     7  27   21      15 20.8          0.00   1005.7
## 111 2017     7  27   21      30 20.4          0.00   1005.6
## 112 2017     7  27   21      45 20.4          0.00   1005.8
## 113 2017     7  27   22       0 20.6          0.00   1005.8
## 114 2017     7  27   22      15 20.5          0.00   1005.9
## 115 2017     7  27   22      30 20.4          0.00   1006.0
## 116 2017     7  27   22      45 20.5          0.00   1005.9
## 117 2017     7  27   23       0 20.5          0.00   1005.9
## 118 2017     7  27   23      15 20.6          0.00   1005.9
## 119 2017     7  27   23      30 20.5          0.00   1006.0
## 120 2017     7  27   23      45 20.5          0.00   1006.0
## 121 2017     7  28    0       0 20.4          0.00   1006.0
##     relative_humidity
## 25                100
## 26                100
## 27                100
## 28                100
## 29                100
## 30                100
## 31                100
## 32                100
## 33                100
## 34                100
## 35                100
## 36                100
## 37                100
## 38                100
## 39                100
## 40                100
## 41                100
## 42                100
## 43                100
## 44                100
## 45                100
## 46                 97
## 47                 84
## 48                 82
## 49                 79
## 50                 78
## 51                 78
## 52                 76
## 53                 76
## 54                 75
## 55                 73
## 56                 65
## 57                 57
## 58                 60
## 59                 53
## 60                 52
## 61                 51
## 62                 51
## 63                 50
## 64                 53
## 65                 52
## 66                 57
## 67                 58
## 68                 59
## 69                 60
## 70                 61
## 71                 65
## 72                 66
## 73                 67
## 74                 66
## 75                 68
## 76                 70
## 77                 68
## 78                 69
## 79                 69
## 80                 71
## 81                 72
## 82                 72
## 83                 77
## 84                 79
## 85                 80
## 86                 82
## 87                 84
## 88                 79
## 89                 99
## 90                100
## 91                100
## 92                100
## 93                100
## 94                100
## 95                100
## 96                100
## 97                100
## 98                100
## 99                100
## 100               100
## 101               100
## 102               100
## 103               100
## 104               100
## 105               100
## 106               100
## 107               100
## 108               100
## 109               100
## 110               100
## 111               100
## 112               100
## 113               100
## 114               100
## 115               100
## 116               100
## 117               100
## 118               100
## 119               100
## 120               100
## 121               100
sta2 <- sta1[25:121,2:10]

plot(sta2$temp)


plot(sta2$precipitation)


sta2$temp<20
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
## [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
which(sta2$temp<20)
## [1] 65 66
temp_min_indis <- which(sta2$temp<20)

temp_min_indis
## [1] 65 66
sta2$precipitation[temp_min_indis]
## [1] 7.01 8.80
sta2$precipitation[sta2$temp<20]
## [1] 7.01 8.80
max(sta2$precipitation)
## [1] 8.8

plot(sta2$temp,sta2$precipitation)


cov(sta2$temp,sta2$precipitation)
## [1] -1.078493
cor(sta2$temp,sta2$precipitation)
## [1] -0.2345707

List


v1 <- c("ahmet","mehmet","hüseyin")
v2 <- c( TRUE, FALSE, FALSE)
m1 <- matrix(1:6 , 3, 2)
df1 <- data.frame(v1,v2,m1)

l1 <- list(v1,v2,m1,df1)
l1
## [[1]]
## [1] "ahmet"   "mehmet"  "hüseyin"
## 
## [[2]]
## [1]  TRUE FALSE FALSE
## 
## [[3]]
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
## 
## [[4]]
##        v1    v2 X1 X2
## 1   ahmet  TRUE  1  4
## 2  mehmet FALSE  2  5
## 3 hüseyin FALSE  3  6

l1[1]
## [[1]]
## [1] "ahmet"   "mehmet"  "hüseyin"
l1[1][1]
## [[1]]
## [1] "ahmet"   "mehmet"  "hüseyin"
l1[[1]]
## [1] "ahmet"   "mehmet"  "hüseyin"
l1[[1]][1]
## [1] "ahmet"

l1[1:2]
## [[1]]
## [1] "ahmet"   "mehmet"  "hüseyin"
## 
## [[2]]
## [1]  TRUE FALSE FALSE
l1[1:2,2:3]
str(l1)
## List of 4
##  $ : chr [1:3] "ahmet" "mehmet" "hüseyin"
##  $ : logi [1:3] TRUE FALSE FALSE
##  $ : int [1:3, 1:2] 1 2 3 4 5 6
##  $ :'data.frame':    3 obs. of  4 variables:
##   ..$ v1: Factor w/ 3 levels "ahmet","hüseyin",..: 1 3 2
##   ..$ v2: logi [1:3] TRUE FALSE FALSE
##   ..$ X1: int [1:3] 1 2 3
##   ..$ X2: int [1:3] 4 5 6

attributes(l1)
## NULL
l1[4]
## [[1]]
##        v1    v2 X1 X2
## 1   ahmet  TRUE  1  4
## 2  mehmet FALSE  2  5
## 3 hüseyin FALSE  3  6
df2 <- l1[4]
df2

df2 <- l1[[4]]
df2

Factor

char1 <- c("a","b","c","a","d","d","a")
fac1 <- factor(char1)
fac1
## [1] a b c a d d a
## Levels: a b c d
fac1[4]
## [1] a
## Levels: a b c d
num1 <- c(1,4,2,5,7,3,2,1,1,1,2)
fac2 <- factor(num1)
fac2
##  [1] 1 4 2 5 7 3 2 1 1 1 2
## Levels: 1 2 3 4 5 7

Table

char1 <- c("a","b","c","a","d","d","a")
tab1 <- table(char1)
tab1
## char1
## a b c d 
## 3 1 1 2
tab1[2]
## b 
## 1
num1 <- c(1,4,2,5,7,3,2,1,1,1,2)
tab2 <- table(num1)
tab2
## num1
## 1 2 3 4 5 7 
## 4 3 1 1 1 1

Netcdf Structure

LINK


Which

which(letters == "g")
## [1] 7
num1 <- c(1,4,2,5,7,3,2,1,1,1,2)
which(num1 != 1)
## [1]  2  3  4  5  6  7 11
which(num1 != c(1,2))
## Warning in num1 != c(1, 2): longer object length is not a multiple of
## shorter object length
## [1]  2  3  4  5  6  7  8 10 11
which(num1 != 1 & num1 != 2)
## [1] 2 4 5 6

head(sta1)
##   sta_no year month day hour minutes temp precipitation pressure
## 1  18397 2017     7  26   18       0 23.9             0   1003.0
## 2  18397 2017     7  26   18      15 23.9             0   1003.1
## 3  18397 2017     7  26   18      30 23.8             0   1003.2
## 4  18397 2017     7  26   18      45 23.8             0   1003.2
## 5  18397 2017     7  26   19       0 23.6             0   1003.2
## 6  18397 2017     7  26   19      15 23.2             0   1003.1
##   relative_humidity
## 1                94
## 2                95
## 3                96
## 4                96
## 5                96
## 6                97
which((sta1$hour == 18) & (sta1$minutes == 15))
## [1]  2 98

m2 <- matrix(1:16,4,4)
m2
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
which.min(m2)
## [1] 1
which(m2-7 > 0)
## [1]  8  9 10 11 12 13 14 15 16

which(m2-7 > 0, arr.ind=TRUE)
##       row col
##  [1,]   4   2
##  [2,]   1   3
##  [3,]   2   3
##  [4,]   3   3
##  [5,]   4   3
##  [6,]   1   4
##  [7,]   2   4
##  [8,]   3   4
##  [9,]   4   4
which(m2-7 > 0, arr.ind=TRUE, useNames = FALSE)
##       [,1] [,2]
##  [1,]    4    2
##  [2,]    1    3
##  [3,]    2    3
##  [4,]    3    3
##  [5,]    4    3
##  [6,]    1    4
##  [7,]    2    4
##  [8,]    3    4
##  [9,]    4    4

which(ll <- c(TRUE, FALSE, TRUE, NA, FALSE, FALSE, TRUE))
## [1] 1 3 7
ll
## [1]  TRUE FALSE  TRUE    NA FALSE FALSE  TRUE
seq(ll)
## [1] 1 2 3 4 5 6 7
letters[seq(ll)]
## [1] "a" "b" "c" "d" "e" "f" "g"

apply Group


apply

apply(X, MARGIN, FUN)

takes data frame or matrix as an input

gives output in vector, list or array



m1 <- matrix( 1:10 , nrow=5, ncol=6)
m1
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    6    1    6    1    6
## [2,]    2    7    2    7    2    7
## [3,]    3    8    3    8    3    8
## [4,]    4    9    4    9    4    9
## [5,]    5   10    5   10    5   10
a_m1 <- apply(m1, 2, sum)
a_m1
## [1] 15 40 15 40 15 40

a_m1
## [1] 15 40 15 40 15 40


lapply

lapply(X, FUN)

takes list, vector or data frame as input

lapply() function does not need MARGIN

gives output in list


v1
## [1] "ahmet"   "mehmet"  "hüseyin"
lapply(v1,max)
## [[1]]
## [1] "ahmet"
## 
## [[2]]
## [1] "mehmet"
## 
## [[3]]
## [1] "hüseyin"

l1
## [[1]]
## [1] "ahmet"   "mehmet"  "hüseyin"
## 
## [[2]]
## [1]  TRUE FALSE FALSE
## 
## [[3]]
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
## 
## [[4]]
##        v1    v2 X1 X2
## 1   ahmet  TRUE  1  4
## 2  mehmet FALSE  2  5
## 3 hüseyin FALSE  3  6

#l1
lapply(l1, FUN = dim)
## [[1]]
## NULL
## 
## [[2]]
## NULL
## 
## [[3]]
## [1] 3 2
## 
## [[4]]
## [1] 3 4

#l1
lapply(l1,"[",1)
## [[1]]
## [1] "ahmet"
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] 1
## 
## [[4]]
##        v1
## 1   ahmet
## 2  mehmet
## 3 hüseyin


l2 <- list(x = 1:5, y = 6:10, z = 11:15)
lapply(l2, FUN = median)
## $x
## [1] 3
## 
## $y
## [1] 8
## 
## $z
## [1] 13

head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
df3 <- cars

lapply(df3, min)
## $speed
## [1] 4
## 
## $dist
## [1] 2

sapply

sapply(X, FUN)

takes list, vector or data frame as input

gives output in vector or matrix


#head(cars)
#df3 <- cars

lapply(df3, min)
## $speed
## [1] 4
## 
## $dist
## [1] 2
sapply(df3, min)
## speed  dist 
##     4     2

tapply

tapply(X, INDEX, FUN = NULL)

for each factor variable in a vector


iris
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1            5.1         3.5          1.4         0.2     setosa
## 2            4.9         3.0          1.4         0.2     setosa
## 3            4.7         3.2          1.3         0.2     setosa
## 4            4.6         3.1          1.5         0.2     setosa
## 5            5.0         3.6          1.4         0.2     setosa
## 6            5.4         3.9          1.7         0.4     setosa
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 150          5.9         3.0          5.1         1.8  virginica

tapply(iris$Sepal.Width, iris$Species, median)
##     setosa versicolor  virginica 
##        3.4        2.8        3.0

mapply

mapply - For when you have several data structures (e.g. vectors, lists) and you want to apply a function to the 1st elements of each, and then the 2nd elements of each

sum(1:5, 1:5, 1:5)
## [1] 45
rep(1:4, 4:1)   # replicates
##  [1] 1 1 1 1 2 2 2 3 3 4

mapply(sum, 1:5, 1:5, 1:5) 
## [1]  3  6  9 12 15
mapply(rep, 1:4, 4:1)   # replicates
## [[1]]
## [1] 1 1 1 1
## 
## [[2]]
## [1] 2 2 2
## 
## [[3]]
## [1] 3 3
## 
## [[4]]
## [1] 4

sweep

m3 <- matrix(4:15,4,3)
m3
##      [,1] [,2] [,3]
## [1,]    4    8   12
## [2,]    5    9   13
## [3,]    6   10   14
## [4,]    7   11   15
m3_mean <- apply(m3, 2, mean)
m3_mean
## [1]  5.5  9.5 13.5
m3_sd <- apply(m3, 2, sd)
m3_sd
## [1] 1.290994 1.290994 1.290994

m3_a <- sweep(m3, 2, m3_mean,"-")
m3_a
##      [,1] [,2] [,3]
## [1,] -1.5 -1.5 -1.5
## [2,] -0.5 -0.5 -0.5
## [3,]  0.5  0.5  0.5
## [4,]  1.5  1.5  1.5
m3_b <- sweep(m3_a, 2, m3_sd, "/")
m3_b
##            [,1]       [,2]       [,3]
## [1,] -1.1618950 -1.1618950 -1.1618950
## [2,] -0.3872983 -0.3872983 -0.3872983
## [3,]  0.3872983  0.3872983  0.3872983
## [4,]  1.1618950  1.1618950  1.1618950

aggregate

sta2
##     year month day hour minutes temp precipitation pressure
## 25  2017     7  27    0       0 22.3          0.00   1003.4
## 26  2017     7  27    0      15 22.2          0.00   1003.2
## 27  2017     7  27    0      30 22.5          0.00   1003.2
## 28  2017     7  27    0      45 22.6          0.00   1003.2
## 29  2017     7  27    1       0 22.6          0.00   1003.3
## 30  2017     7  27    1      15 22.6          0.00   1003.4
## 31  2017     7  27    1      30 22.6          0.00   1003.2
## 32  2017     7  27    1      45 22.7          0.00   1003.2
## 33  2017     7  27    2       0 22.6          0.00   1003.3
## 34  2017     7  27    2      15 22.5          0.00   1003.2
## 35  2017     7  27    2      30 22.6          0.00   1003.2
## 36  2017     7  27    2      45 22.5          0.00   1003.1
## 37  2017     7  27    3       0 22.5          0.00   1003.1
## 38  2017     7  27    3      15 22.4          0.00   1003.0
## 39  2017     7  27    3      30 22.5          0.00   1003.1
## 40  2017     7  27    3      45 22.4          0.00   1003.3
## 41  2017     7  27    4       0 22.5          0.00   1003.4
## 42  2017     7  27    4      15 22.6          0.00   1003.5
## 43  2017     7  27    4      30 23.0          0.00   1003.5
## 44  2017     7  27    4      45 23.2          0.00   1003.5
## 45  2017     7  27    5       0 24.2          0.00   1003.6
## 46  2017     7  27    5      15 25.1          0.00   1003.5
## 47  2017     7  27    5      30 25.5          0.00   1003.4
## 48  2017     7  27    5      45 26.1          0.00   1003.3
## 49  2017     7  27    6       0 27.1          0.00   1003.3
## 50  2017     7  27    6      15 26.9          0.00   1003.3
## 51  2017     7  27    6      30 27.6          0.00   1003.3
## 52  2017     7  27    6      45 28.0          0.00   1003.2
## 53  2017     7  27    7       0 28.4          0.00   1003.1
## 54  2017     7  27    7      15 28.5          0.00   1003.1
## 55  2017     7  27    7      30 29.3          0.00   1003.0
## 56  2017     7  27    7      45 30.2          0.00   1002.9
## 57  2017     7  27    8       0 30.1          0.00   1002.8
## 58  2017     7  27    8      15 30.1          0.00   1002.8
## 59  2017     7  27    8      30 30.4          0.00   1002.8
## 60  2017     7  27    8      45 30.4          0.00   1002.8
## 61  2017     7  27    9       0 30.8          0.00   1002.9
## 62  2017     7  27    9      15 30.9          0.00   1002.8
## 63  2017     7  27    9      30 31.0          0.00   1002.6
## 64  2017     7  27    9      45 31.5          0.00   1002.6
## 65  2017     7  27   10       0 31.2          0.00   1002.6
## 66  2017     7  27   10      15 30.9          0.00   1002.4
## 67  2017     7  27   10      30 30.9          0.00   1002.4
## 68  2017     7  27   10      45 30.4          0.00   1002.3
## 69  2017     7  27   11       0 30.4          0.00   1002.1
## 70  2017     7  27   11      15 30.0          0.00   1001.9
## 71  2017     7  27   11      30 29.2          0.00   1001.9
## 72  2017     7  27   11      45 29.5          0.00   1001.7
## 73  2017     7  27   12       0 29.4          0.00   1001.6
## 74  2017     7  27   12      15 29.3          0.00   1001.3
## 75  2017     7  27   12      30 29.6          0.00   1001.2
## 76  2017     7  27   12      45 28.8          0.00   1001.3
## 77  2017     7  27   13       0 29.0          0.00   1001.1
## 78  2017     7  27   13      15 29.0          0.00   1001.2
## 79  2017     7  27   13      30 29.2          0.00   1001.3
## 80  2017     7  27   13      45 28.4          0.00   1001.5
## 81  2017     7  27   14       0 27.8          0.00   1001.6
## 82  2017     7  27   14      15 27.4          0.00   1001.6
## 83  2017     7  27   14      30 26.6          0.00   1001.5
## 84  2017     7  27   14      45 26.2          0.00   1001.2
## 85  2017     7  27   15       0 25.8          0.00   1001.1
## 86  2017     7  27   15      15 25.6          0.00   1001.0
## 87  2017     7  27   15      30 25.4          0.00   1000.9
## 88  2017     7  27   15      45 24.2          0.00   1001.8
## 89  2017     7  27   16       0 19.2          7.01   1003.7
## 90  2017     7  27   16      15 19.5          8.80   1003.2
## 91  2017     7  27   16      30 20.1          0.25   1003.1
## 92  2017     7  27   16      45 20.8          0.00   1003.7
## 93  2017     7  27   17       0 21.2          1.13  -9999.0
## 94  2017     7  27   17      15 21.4          0.02   1005.6
## 95  2017     7  27   17      30 21.4          1.25   1005.4
## 96  2017     7  27   17      45 21.4          2.75   1005.1
## 97  2017     7  27   18       0 21.2          0.00   1005.1
## 98  2017     7  27   18      15 21.0          0.00  -9999.0
## 99  2017     7  27   18      30 20.8          0.00   1006.3
## 100 2017     7  27   18      45 20.9          0.00  -9999.0
## 101 2017     7  27   19       0 20.8          0.19   1005.7
## 102 2017     7  27   19      15 20.7          0.00   1006.2
## 103 2017     7  27   19      30 20.8          0.20   1003.6
## 104 2017     7  27   19      45 20.8          0.22   1003.7
## 105 2017     7  27   20       0 20.9          0.00  -9999.0
## 106 2017     7  27   20      15 20.6          0.00  -9999.0
## 107 2017     7  27   20      30 20.6          0.00   1005.1
## 108 2017     7  27   20      45 20.5          0.00   1005.6
## 109 2017     7  27   21       0 20.7          0.00   1005.5
## 110 2017     7  27   21      15 20.8          0.00   1005.7
## 111 2017     7  27   21      30 20.4          0.00   1005.6
## 112 2017     7  27   21      45 20.4          0.00   1005.8
## 113 2017     7  27   22       0 20.6          0.00   1005.8
## 114 2017     7  27   22      15 20.5          0.00   1005.9
## 115 2017     7  27   22      30 20.4          0.00   1006.0
## 116 2017     7  27   22      45 20.5          0.00   1005.9
## 117 2017     7  27   23       0 20.5          0.00   1005.9
## 118 2017     7  27   23      15 20.6          0.00   1005.9
## 119 2017     7  27   23      30 20.5          0.00   1006.0
## 120 2017     7  27   23      45 20.5          0.00   1006.0
## 121 2017     7  28    0       0 20.4          0.00   1006.0
##     relative_humidity
## 25                100
## 26                100
## 27                100
## 28                100
## 29                100
## 30                100
## 31                100
## 32                100
## 33                100
## 34                100
## 35                100
## 36                100
## 37                100
## 38                100
## 39                100
## 40                100
## 41                100
## 42                100
## 43                100
## 44                100
## 45                100
## 46                 97
## 47                 84
## 48                 82
## 49                 79
## 50                 78
## 51                 78
## 52                 76
## 53                 76
## 54                 75
## 55                 73
## 56                 65
## 57                 57
## 58                 60
## 59                 53
## 60                 52
## 61                 51
## 62                 51
## 63                 50
## 64                 53
## 65                 52
## 66                 57
## 67                 58
## 68                 59
## 69                 60
## 70                 61
## 71                 65
## 72                 66
## 73                 67
## 74                 66
## 75                 68
## 76                 70
## 77                 68
## 78                 69
## 79                 69
## 80                 71
## 81                 72
## 82                 72
## 83                 77
## 84                 79
## 85                 80
## 86                 82
## 87                 84
## 88                 79
## 89                 99
## 90                100
## 91                100
## 92                100
## 93                100
## 94                100
## 95                100
## 96                100
## 97                100
## 98                100
## 99                100
## 100               100
## 101               100
## 102               100
## 103               100
## 104               100
## 105               100
## 106               100
## 107               100
## 108               100
## 109               100
## 110               100
## 111               100
## 112               100
## 113               100
## 114               100
## 115               100
## 116               100
## 117               100
## 118               100
## 119               100
## 120               100
## 121               100

aggregate(sta2$temp, by=sta2["hour"], FUN=sum)
##    hour     x
## 1     0 110.0
## 2     1  90.5
## 3     2  90.2
## 4     3  89.8
## 5     4  91.3
## 6     5 100.9
## 7     6 109.6
## 8     7 116.4
## 9     8 121.0
## 10    9 124.2
## 11   10 123.4
## 12   11 119.1
## 13   12 117.1
## 14   13 115.6
## 15   14 108.0
## 16   15 101.0
## 17   16  79.6
## 18   17  85.4
## 19   18  83.9
## 20   19  83.1
## 21   20  82.6
## 22   21  82.3
## 23   22  82.0
## 24   23  82.1

Functions

celsius_to_kelvin <- function(temp_C) {
  temp_K <- temp_C + 273.15
  return(temp_K)
}

celsius_to_kelvin(19.5)
## [1] 292.65
sum.of.squares <- function(x,y) {
  x^2 + y^2
}

sum.of.squares(3,5)
## [1] 34

red.plot <- function(x, y) {
  plot(x, y, col="red")
}

red.plot(runif(20), runif(20))


red.plot <- function(x, y, ...) {
  plot(x, y, col="red", ...)
}

red.plot(runif(20), runif(20), xlab="My x axis", ylab="My y axis", main="redddd")


red.plot <- function(x, y, ...) {
  plot(x, y, col="red", ...)
}

red.plot(sta1$temp, sta1$relative_humidity, xlab="My x axis", ylab="My y axis", main="redddd")


lets_guess <- function(x)  
{  
  n=28
  if(missing(x)) print("enter a value");
  if(x<0 | x>40) print("value must be 0<=x<=40)")
  else if(x<n) print("enter a higher value")
  else if(x>n) print("enter a lower value")
  else if(x==n) print(c("you got it ",x))
  else print("what are you doing? guess a number")
}

String Manipulation

  • grep()
  • nchar()
  • paste()
  • sprintf()
  • substr()
  • strsplit()
  • regex()
  • gregexpr()

grep("b+", c("abc", "bda", "cca a", "abd"))
## [1] 1 2 4
str <- "Big Data at DataFlair"
nchar(str)
## [1] 21
paste("Hadoop", "Spark", "and", "Flink")
## [1] "Hadoop Spark and Flink"
paste("Hadoop", "Spark", "and", "Flink",sep = "")
## [1] "HadoopSparkandFlink"

num <- "12345678"
substr(num, 4, 5)
## [1] "45"
str = "Splitting sentence into words"
strsplit(str, " ")
## [[1]]
## [1] "Splitting" "sentence"  "into"      "words"
strsplit(str, "i")
## [[1]]
## [1] "Spl"          "tt"           "ng sentence " "nto words"

x <- "Learning To MANIPULATE strinGS in R"

tolower(x)
## [1] "learning to manipulate strings in r"
toupper(x)
## [1] "LEARNING TO MANIPULATE STRINGS IN R"
x <- "This is A string."

chartr(old = "A", new = "a", x)
## [1] "This is a string."

y <- "Tomorrow I plzn do lezrn zbout dexduzl znzlysis."

chartr(old = "dz", new = "ta", y)
## [1] "Tomorrow I plan to learn about textual analysis."
streets <- c("Main", "Elm", "Riverbend", "Mario", "Frederick")
abbreviate(streets)
##      Main       Elm Riverbend     Mario Frederick 
##    "Main"     "Elm"    "Rvrb"    "Mari"    "Frdr"
abbreviate(streets, minlength = 2)
##      Main       Elm Riverbend     Mario Frederick 
##      "Mn"      "El"      "Rv"      "Mr"      "Fr"