Date : 29/01/2020


R Language - Indexing - 1D & 2D Data

  1. Open your R-Studio, create a new “R Project” and “R Script” file and save it
  2. Learn your Working Directory and check files, under your working directory
  3. Solve this math. Is your solution bigger than 4 ? TRUE or FALSE.
  4. Create a vector with six numeric and two integer elements, and than assign it to a new variable (vec1)
  5. Check the class vec1. Coerce vec1 to be a character vector, assign it as vec2, and check the class vec2
  6. Create a vector with eight numeric elements using sequence function, and assign it to a new variable (vec3)
  7. Check the length and calculate the mean of vec3.
  8. Print the second element of vec3 and change it as "TRUE
  9. Create a new vector with 16 elements using the function that generate random uniform numbers, and assign it as vec4.
  10. Create a new matrix, with 3 rows and 8 columns using vec3 and vec4 by row. Assign it as mat1.
  11. Create a new array with 6 rows, 4 columns and 2 layers using mat1. Assign it as arr1
  12. Check structure and dimensions of mat1 and arr1.
  13. Select 3rd row, 2nd column and change it with NA for each layers
  14. Create a data frame with vec1, vec2 and vec3. Assign it as df1. Print 2nd column of df1.
  15. Create a list with vec1, mat1, arr1 and df1. Assign it as list1. Print 4th element of list1.
  16. Go to main web page and download Istanbul_Cekmekoy_Omerli_26072017-29072017_15min.txt in your working directory and Read the station data in R-Studio (be careful about file path, header and seperator). Assign it as sta_data1
  17. Check the structure and attributes of sta_data. Print and plot the precipiptation and temperature of sta_data1.
  18. Change temperature with NA if the value is lower than 20. Assign it as sta_data2
  19. Write sta_data2 as a new txt file in your working directory.
  20. Install “ncdf4” and “RNetCDF” package and call it into R-Studio from library.

R Programming - Statistics - Visualization - 3D Data

  1. Check the clock and assign it as a new variable named clock (e.g. 11,23). Write a if condition. If the clock is between 10 and 12, print “I am in a zoom meeting”, else print “I should join a zoom meeting”.
if ( ... ) {
   print( ... )
} else {
   print( ... )
} 
  1. Write a loop. Print i for each value from 4 to 11 in for loop.
for ( ... in ... ) { 
     print( ... ) 
}
  1. Assign temperature of sta_data1 as temp1. Write an nested if-else condition in for loop. Do these:
for (i in 1:length( ... )) {
  if (temp1[i] < ... & ... ) {
    print(c(temp1[i], ... ))
  } else if ( ... ) {
    print( ... )
  } else { ... }
}
  1. Write a function with named “outlier”. Do these into function;

NOTE : Condition for outliers is: OUTLIERS < MEAN-IQR(temp1) or OUTLIERS > MEAN+IQR(temp1)

outlier <- function( x ) {

mean(...)
median(...)
...
...
...

  for (i in 1:length(x)) {
    if ( ... ) {
      print(c( ... , ... ))
      print(which(...))
    }
  }
}

  1. Go to main web page and download CRU_TR_Near-Surface_Temp_16-01-1901_16-12-2012_Monthly.nc in your working directory and read the station data in R-Studio (with ncdf4 or RNetCDF Package). Assign it as cru_data1.
  2. Check the metadata, structure, class and attributes of cru_data1.
  3. Write these to me as messages:
message(" ... variable(s) and ... dimension(s) ")
message("dimensions are ..., ..., ...")
message("the short name of variable is ..., the long name of variable is ... ")
message("the size of time step is ... ")
message("Time ends in  ... / ... ")
  1. Get attributes of variable from cru_data1 data (with ncdf4 or RNetCDF Package).
  2. Get the temperature variable from cru_data1 data (with ncdf4 or RNetCDF Package). Assign it as var1.
  3. Check the structure, class and dimensions of var1.
  4. Print var1 at 2nd Time step for all Latitude, Longitude.
  5. Image the Turkey Temperature Map for November 2010. (with latitudes and longitudes - 2D map) (be careful about the size of time step and temporal resolution of cru_data1)
  6. Can you find the Latitude and Longitude grid (or index) number of Istanbul, approximately ? (e.g., var1[2,34,])
  7. Calculate the mean of var1 for Istanbul grid (or index) for ALL time.
  8. Plot the values of var1 time series for ALL time considering the Istanbul grid (hint: use the index of Istanbul, e.g., var1[2,34,]).
  9. Can you plot (or Image) the mean of Turkey Temperature Map for ALL time (hint: use the apply function)

  1. If you are here, then special thanks to you for visiting my GitHub page, and our official website ( Hint: You are special! )