I have a data set titled 'google_removal' with 5 columns and 14,468 rows. I am trying to expand the number of rows based on the values which specifies the frequency for observations in each row. However, I keep getting the 'undefined columns selected' error message. As far as I can see it is not a problem that stems from forgetting to add a comma and I have checked that the column in question has the particular heading I am using.
Frequency is specified in the the column 'total'. It is numerical. The other variables are character. Very much a beginner here so apologies if question is poorly structured.
Data looks like this. Happy to provide more if needed.
'period ending' 'country' 'code' 'product' 'reason' 'total' 'year'
2011-06-30 Argentina AR Blogger Defamation 4 2011
2011-06-30 Argentina AR Blogger Privacy 1 2011
2011-06-30 Argentina AR GoogleAd Defamation 1 2011
2011-06-30 Argentina AR WebSearch Defamation 6 2011
I was trying to achieve the following:
'period_ending' 'country' 'code' 'product' 'reason' 'year'
2011-06-30 Argentina AR Blogger Defamation 2011
2011-06-30 Argentina AR Blogger Defamation 2011
2011-06-30 Argentina AR Blogger Defamation 2011
2011-06-30 Argentina AR Blogger Defamation 2011
2011-06-30 Argentina AR Blogger Privacy 2011
2011-06-30 Argentina AR GoogleAd Defamation 2011
2011-06-30 Argentina AR WebSearch Defamation 2011
2011-06-30 Argentina AR WebSearch Defamation 2011
2011-06-30 Argentina AR WebSearch Defamation 2011
2011-06-30 Argentina AR WebSearch Defamation 2011
2011-06-30 Argentina AR WebSearch Defamation 2011
2011-06-30 Argentina AR WebSearch Defamation 2011
I have tried the following ('total' is the heading of the column I want to use for expanding the number of rows) adapting it from an answer to a question on expanding rows.
google_removal_expanded <- google_removal[seq_len(nrow(google_removal), google_removal$total), 1:13000]
google_removal_expanded <- google_removal[rep(row.names(google_removal), google_removal$total), 1:13000]
I was expecting either of these to return the expanded data frame but I instead received variations of
"Error in
[.data.frame
(google_removal, rep(row.names("total"), google_removal$total), : undefined columns selected" for both.
Grateful for any assistance. My attempt to solve the problem adapted the answers in Repeat each row of data.frame the number of times specified in a column
Here is some data to try it with:
dput(head(google_removal))
structure(list(period_ending = c("2011-06-30", "2011-06-30",
"2011-06-30", "2011-06-30", "2011-06-30", "2011-06-30"), country = c("Argentina",
"Argentina", "Argentina", "Argentina", "Argentina", "Argentina"
), code = c("AR", "AR", "AR", "AR", "AR", "AR"), product = c("Blogger",
"Blogger", "Google Ads", "Web Search", "Web Search", "Web Search"
), reason = c("Defamation", "Privacy and Security", "Defamation",
"Defamation", "Other", "Privacy and Security"), total = c(4L,
1L, 1L, 6L, 1L, 8L), year = c("2011", "2011", "2011", "2011",
"2011", "2011")), row.names = c(NA, 6L), class = "data.frame")