Multiple transpose in R -


i have data.frame this:

country country b country c country d 123 567 789 101 asd dfa dgf fgh 

i want convert this:

country      123      asd country b      567      dfa country c      789      dgf country d      101      fgh 

data.frame contains lot of row, can't subset according rownames. t function convert this:

country countryb countryc countryd 123 567 789 101 asd dfa gf fgh  

so, t not useful want.

one way of splitting data frame using matrix. i'll assume know final number of columns (ncols).

# data frame example df = data.frame(x = c("country a","country b","country c","country d",                          "123","567","789","101","asd","dfa","dgf","fgh"),stringsasfactors = f)  # ncols: known number of columns ncols = 3    df = data.frame(matrix(df$x, dim(df)[1]/ncols, ncols), stringsasfactors = f) 

result:

> df          x1  x2  x3 1 country 123 asd 2 country b 567 dfa 3 country c 789 dgf 4 country d 101 fgh 

if use approach, keep in mind number of rows of original data frame should multiple of number of columns. if not, warning , missing data filled using original df beginning


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -