Reading and Writing Lists and Tables

To achieve the solution, some packages are required. The first one is the package tidyverse which makes it possible to work with lists and tables. The second package is readxl which makes it possible to read/write Excel files. Once the packages are installed, the Excel file with the data is loaded, and using a for-loop which iterates through the rows of data, the information is extracted from each row and then saved into three lists, depending on the variable type in the given data. Variable Type can have three values: Realtor Sale, Foreclosure, and Owner Sale. The program will create three lists and save the variable Price in one of three lists, depending on the value of Type.

Finally, after all, rows were analyzed, the three lists are saved into a new .csv file.

# library(tidyverse) library("readxl") install.packages("writexl") my_data <- read_excel("Real Estate - pivot.xlsx") column_names = distinct(my_data['Type']) list1 = list() #list1 = c(list1, "Realtor Sale") list2 = list() #list2 = c(list2, "Foreclosure") list3 = list() #list3 = c(list3, "Owner Sale") for(row in 1:nrow(my_data)) { type = my_data[row, "Type"] price = my_data[row, "Price"] if(type == "Realtor Sale") list1 = c(list1, price) else if(type == "Foreclosure") list2 = c(list2, price) else if(type == "Owner Sale") list3 = c(list3, price) } out = data.frame('Realtor Sale' = list1, 'Foreclosure' = list2, 'Owner Sale' = list3) write.table(out, file = "Output.csv", sep = ",", col.names = NA, qmethod = "double")