I am working with a data frame that comes from the database in the following way:
username elements
username1 """interfaces"".""dual()"""
username1 """interfaces"".""f_capitalaccrualcurrentyear"""
username2 """interfaces"".""dnow_completion"",""interfaces"".""dnow_s_daily_prod_ta"""
username2 """interfaces"".""dnow_completion"",""interfaces"".""dnow_s_daily_prod_ta"""
username2 """interfaces"".""dnow_completion"",""interfaces"".""dnow_s_daily_prod_ta"""
username4 """interfaces"".""dnow_s_downtime_stat_with_lat_long"""
username3 """interfaces"".""dnow_completion"",""interfaces"".""dnow_s_daily_prod_ta"""
So, two columns, a "username" and "elements". So there can be one element or several elements the user has used in one transaction. When multiple elements, they are separated with a comma in a transaction. I need to have the elements separated, one per row, but still tagged with the user name. At the end I'd like it to be like so:
username elements
username1 """interfaces"".""dual()"""
username1 """interfaces"".""f_capitalaccrualcurrentyear"""
username2 """interfaces"".""dnow_completion""
username2 ""interfaces"".""dnow_s_daily_prod_ta"""
username2 """interfaces"".""dnow_completion""
username2 ""interfaces"".""dnow_s_daily_prod_ta"""
username2 """interfaces"".""dnow_completion""
username2 ""interfaces"".""dnow_s_daily_prod_ta"""
username4 """interfaces"".""dnow_s_downtime_stat_with_lat_long"""
username3 """interfaces"".""dnow_completion""
username3 ""interfaces"".""dnow_s_daily_prod_ta"""
I have been trying to iterate through the data frame, split the elements that have commas and then put them back together with the respective user name.
I have been trying the code below but it is super inefficient. I am new to "R" so my guess is that there has to be a more efficient way to do this.
interface.data <-data.frame(
username = c(),
elements = c()
)
for (row in 1:nrow(input)) { ##input is the frame that comes from the database
myrowbrk<-input[row,"elements"]
myrowelements<-chartr(",", "\n", myrowbrk)
user<-input[row,"username"]
interface.newdata <- data.frame(
username = user,
elements = c(myrowelements)
)
interface.final<- rbind(interface.data,interface.newdata )
}
output<-interface.final