Skip to content
Prev 308097 / 398503 Next

Pivot Table "like" structure

On Oct 13, 2012, at 5:38 PM, Bhupendrasinh Thakre wrote:

            
You are apparently using attach() on your dataframe. That will often  
create confusion. Better to use with():

?with

For your problem without `attach` these are available:

 > table(dat$Char2)

ABCD EFGH FGHJ GGGG
    2    3    1    2


 > aggregate(dat$Char2, dat['Char2'], length)
   Char2 x
1  ABCD 2
2  EFGH 3
3  FGHJ 1
4  GGGG 2

With attach() in effect for your dataframe this would have worked as  
well:

 > aggregate(Char2, list(Char2), length)
   Group.1 x
1    ABCD 2
2    EFGH 3
3    FGHJ 1
4    GGGG 2

'count' is not a base R function, although it may be available in some  
packages. If you have other packages you are loading, you should name  
them.

If you want to get tabulations of all the columns that have "Char" in  
their names

 > sapply(dat[ grep("Char", names(dat)) ], table)
$Char1

A B C D M
2 2 2 1 1

$Char2

ABCD EFGH FGHJ GGGG
    2    3    1    2

$Char3

  ASDFG  DDDDD EEEEEE
      4      2      2