Let’s say that I want to generate a large data frame from scratch.
Using the data.frame function is how I would generally create data frames.
However, df’s like the following are extremely error prone and inefficient.
So is there a more efficient way of creating the following data frame.
df <- data.frame(GOOGLE_CAMPAIGN=c(rep("Google - Medicare - US", 928), rep("MedicareBranded", 2983),
rep("Medigap", 805), rep("Medigap Branded", 1914),
rep("Medicare Typos", 1353), rep("Medigap Typos", 635),
rep("Phone - MedicareGeneral", 585),
rep("Phone - MedicareBranded", 2967),
rep("Phone-Medigap", 812),
rep("Auto Broad Match", 27),
rep("Auto Exact Match", 80),
rep("Auto Exact Match", 875)),
GOOGLE_AD_GROUP=c(rep("Medicare", 928), rep("MedicareBranded", 2983),
rep("Medigap", 805), rep("Medigap Branded", 1914),
rep("Medicare Typos", 1353), rep("Medigap Typos", 635),
rep("Phone ads 1-Medicare Terms",585),
rep("Ad Group #1", 2967), rep("Medigap-phone", 812),
rep("Auto Insurance", 27),
rep("Auto General", 80),
rep("Auto Brand", 875)))
Yikes, that is some ‘bad’ code. How can I generate this ‘large’ data frame in a more efficient manner?
If your only source for that information is a piece of paper, then you probably won’t get much better than that, but you can at least consolidate all that into a single
repcall for each column:which should be the same:
But if this information is already in a data structure in R somehow and you just need to transform it, that could possibly be even easier, but we’d need to know what that structure is.