Home > OS >  Table reformating
Table reformating

Time:01-29

I have this table with two columns, major_activity_area and word_stem.

major_activity_area word_stem
Youth Development program
Youth Development girl
Youth Development youth
Youth Development school
Religion Related Spiritual Development service
Religion Related Spiritual Development provid
Religion Related Spiritual Development program
Religion Related Spiritual Development hous

What I want to do is to make major_Activity_areas new columns and word_stem words to be listed under each columns. Such as:

youth development. Religion Related Spiritual Development
program. servic
girl. provid
youth program
school hous

I would appreciate any help! :)

CodePudding user response:

Try the transpose function t()

since you did not give a sample dataset dput()

I'll just create a dummy dataframe from mtcars

df.1<-mtcars%>%
  head(10)

                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

using the transpose function in r, t(dataframe)

df.2 <- t(df.1) 

which uses the first column as headers and gives the result below

   Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout Valiant Duster 360 Merc 240D
mpg      21.00        21.000      22.80         21.400             18.70   18.10      14.30     24.40
cyl       6.00         6.000       4.00          6.000              8.00    6.00       8.00      4.00
disp    160.00       160.000     108.00        258.000            360.00  225.00     360.00    146.70
hp      110.00       110.000      93.00        110.000            175.00  105.00     245.00     62.00
drat      3.90         3.900       3.85          3.080              3.15    2.76       3.21      3.69
wt        2.62         2.875       2.32          3.215              3.44    3.46       3.57      3.19
qsec     16.46        17.020      18.61         19.440             17.02   20.22      15.84     20.00
vs        0.00         0.000       1.00          1.000              0.00    1.00       0.00      1.00
am        1.00         1.000       1.00          0.000              0.00    0.00       0.00      0.00
gear      4.00         4.000       4.00          3.000              3.00    3.00       3.00      4.00
carb      4.00         4.000       1.00          1.000              2.00    1.00       4.00      2.00
     Merc 230 Merc 280
mpg     22.80    19.20
cyl      4.00     6.00
disp   140.80   167.60
hp      95.00   123.00
drat     3.92     3.92
wt       3.15     3.44
qsec    22.90    18.30
vs       1.00     1.00
am       0.00     0.00
gear     4.00     4.00
carb     2.00     4.00

If this is not what you're looking for, kindly share a sample dataset to be able to produce exactly what you want using dput()

CodePudding user response:

Assuming the str of data is tibble/ data frame, the function "pivot_wider" from tidyverse package would be a solution. I named your data as formal name (df) then calling the function. The code is like this :

library(tidyverse)


 dfw <-  pivot_wider( df, names_from = 
     'major_activity_area',values_from = 'word_stem') %>% unnest()

dfw


                               
  • Related