I've the data which looks like the following:
Var1 Var1234 Var234 Var123456 Var12345678
A 26 19 23 19
B 25 10 11 27
C 21 19 21 11
D 29 18 22 26
E 16 27 11 10
The reason for putting the weird looking variable name is to show that variable length are not same. I want to convert this data into following shape:
Var1 Var_names Values
A Var1234 26
B Var1234 25
C Var1234 21
D Var1234 29
E Var1234 16
A Var234 19
B Var234 10
C Var234 19
D Var234 18
E Var234 27
A Var123456 23
B Var123456 11
C Var123456 21
D Var123456 22
E Var123456 11
A Var12345678 19
B Var12345678 27
C Var12345678 11
D Var12345678 26
E Var12345678 10
I was trying the following code:
new_data <- reshape2::melt(old_data, id = "Var1")
But it's giving the following error:
Using var1 as id variables
Error in match.names(clabs, names(xi)) :
names do not match previous names
Reshape function used:
new_data <- reshape(old_data, direction ="long", varying = list(names(old_data)[2:ncol(old_data)]), v.names="Value", idvar = "Var1")
Error:
Error in `row.names<-.data.frame`(`*tmp*`, value = paste(d[, idvar], times[1L], :
invalid 'row.names' length
I followed this option, but it didn't help me.
Can anyone please tell me how to resolve the issue?
Thank you!