Skip to content

Instantly share code, notes, and snippets.

@scottpham
Created August 3, 2015 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottpham/d3caf52f2139ecf6e128 to your computer and use it in GitHub Desktop.
Save scottpham/d3caf52f2139ecf6e128 to your computer and use it in GitHub Desktop.
#create new (empty) data frame for output
df <- data.frame()
#read source dataframe
sourceData <- read.csv("originals/rentBoard-clean.csv", stringsAsFactors = F)
#get max rows
maxRows <- nrow(sourceData)
for(i in 1:maxRows){
#subset row
row <- sourceData[i,]
#subset scell
name_cell <- row[,"Landlord.Name"]
if(str_detect(name_cell, "\n") ){
#split column into list of character vectors
names <- suppressWarnings(strsplit(name_cell, '\n'))
#for each in character vector
for(name in names[[1]]){
#duplicate source row, replacing ['column'] value with each
row$Landlord.Name = name
#write this row to the new dataframe
df <- rbind(df, row)
}
}
#else write original row to empty data frame
else df <- rbind(df, row)
print(i)
}
#write new data frame to output
write.csv(df, "output/rent_board_split.csv", row.names=FALSE)
print("DUNZO")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment