Home > Blockchain >  How to create a dataframe from mutiple table made from table$ function?
How to create a dataframe from mutiple table made from table$ function?

Time:07-26

I have a dataframe df with column names ag,bg,cg,dg,eg. I made count of variables in each column using df$ function.

table(df$ag)
table(df$bg)
table(df$cg)
table(df$dg)
table(df$eg)

and the output was:

High      Low Moderate 
 648      628      628 

High      Low Moderate 
 648      628      628 

High      Low Moderate 
 648      628      628 

High      Low Moderate 
 648      628      628 

High      Low Moderate 
 648      628      628 

I want to create another datafrmae with these values as :

High    Low Moderate    Group
648     628 628         ag
648     628 628         bg
648     628 628         cg
648     628 628         dg
648     628 628         eg

Thanks !!

CodePudding user response:

You can try as follows:

new = as.data.frame(t(sapply(df[,c('ag','bg','cg','dg','eg')], table)))

new

    High    Low Moderate
   <int>    <int>   <int>
ag  648      628    628
bg  648      628    628
cg  648      628    628
dg  648      628    628
eg  648      628    628
  • Related