Beruflich Dokumente
Kultur Dokumente
Step 1:
For each of the variables, I wrote a function that takes in the name of each file that
corresponds to a certain pattern, and it returns a data frame of just that one file. There is a loop
that checks whether the merged dataset exists currently or not and also feeds the function each
filename. Each variable has its own loop. I know this is a tedious way of doing it, but I couldnt
figure out how to do it with an apply loop that takes in all the different patterns and returns seven
different data frames. To save paper, I just showed the loop once in the code appendix, but I
colored the things I changed every time in red. I changed only these two things when running the
loop for each variable and named them the same thing as the filenames.
Step 2:
To check that all the dates, latitudes, and longitudes correspond, I wrote a loop that went
through all the rows in each data set. It compares the dates, longitudes and latitudes for each row
to make sure that they are all in the right order and it is possible to cbind them in the future.
Since there was no error message that printed, I went ahead and made the master data set.
Step 3:
This loop creates a new column and fills it with the info in each cell in the .dat file.
Step 4:
1. Temperature vs. Pressure
2.
Values of Temperature over Time (at the four corners of the spatial grid)
Code Appendix
# ===== Step 1 =====
filenames <- list.files()
part1 <- function(filename)
{
# Read in original file
singlefileorig <- readLines(filename)
# Chop off all metadata
singlefile <- singlefileorig[8:31]
temp_data <- strsplit(singlefile, "\\s+")
# Organize observations into a 2D matrix
temp_matrix1 <- matrix(unlist(temp_data), ncol = 28, byrow = TRUE)
temp_matrix <- temp_matrix1[,5:28]
class(temp_matrix) <- "numeric"
# Make empty "final" dataframe filled with zeros
temp_final <- as.data.frame(matrix(0, nrow = 576, ncol = 3))
colnames(temp_final) <- c("obs", "olats", "olongs")
# Fill in final dataframe, include data locations as temporary lat/long
a <- 1 #basically the counter
for(i in 1:24){
for(j in 1:24){
temp_final$olats[a] <- i
temp_final$olongs[a] <- j
temp_final$obs[a] <- temp_matrix[i,j]
a <- a + 1
}
}
# Get a list of all the lats and longs in order
longs <- singlefileorig[6]
longs <- strsplit(longs, "\\s+")
longs <- matrix(unlist(longs), ncol = 1, nrow = 25)
longs <- longs[-1,]
lats <- temp_matrix1[,2]
# Assign actual lat/long data to the final dataframe
for(k in 1:576){
temp_final$alats[k] <- lats[temp_final$olats[k]]
temp_final$alongs[k] <- longs[temp_final$olongs[k]]
}
4