I’m importing a csv file that consists of a crosstab with column names in a two-row hierarchy. When I get the table in R, the result looks like this:
alpha X.1 X.2 beta X.1 X.2 X.3 gamma X.1
var1 var2 var3 var1 var2 var3 var4 var1 var4
1 21 50 5 22 48 6 8 25 8
2 27 50 5 24 48 6 8 33 8
3 26 50 5 28 48 6 8 33 8
4 25 50 5 28 48 6 8 20 8
Here, alpha, beta, and gamma are all one level of the hierarchy, while var1, var2, var3, and var4 are the second level.
What I would like it do is get output like the following, where the row names are concatenated but also keeping in mind the structure of the data.
alpha_var1 alpha_var2 alpha_var3 beta_var1 beta_var2 beta_var3 beta_var4 gamma_var1 gamma_var4
1 21 50 5 22 48 6 8 25 8
2 27 50 5 24 48 6 8 33 8
3 26 50 5 28 48 6 8 33 8
4 25 50 5 28 48 6 8 20 8
Any ideas here? Haven’t been able to find anything to deal with this issue. Thanks in advance.
This seems to work, though the use of
xtsseems a bit heavy handed for thena.locf()function, but I know it works and use it frequently, so that’s what I used.