[R] From table to data.frame

arun smartpink111 at yahoo.com
Thu Jan 24 19:06:41 CET 2013


Hi,
In the ?cut(), you can specify `labels`.

#or, you can try:
test.df<-read.table(text="
    Var1 Freq
1    (0,0.05]  68
2  (0.05,0.1]  87
3  (0.1,0.15]  85
4  (0.15,0.2]  72
5  (0.2,0.25]  65
6  (0.25,0.3]  73
7  (0.3,0.35]  74
",sep="",header=T,stringsAsFactors=F)
test.df[,1]<-as.numeric(gsub(".*,(.*)]","\\1",test.df[,1]))
test.df
#  Var1 Freq
#1 0.05   68
#2 0.10   87
#3 0.15   85
#4 0.20   72
#5 0.25   65
#6 0.30   73
#7 0.35   74
A.K.




----- Original Message -----
From: Wim Kreinen <wkreinen at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Thursday, January 24, 2013 12:39 PM
Subject: [R] From table to data.frame

Hello,

I have a dataframe (test.df) with intervals that were generated by table
(see below). I would prefer a dataframe labeled like this:


> test.tab <- table(cut(skn.test$density, seq (0,1, by=0.05)))
> test.tab

  (0,0.05] (0.05,0.1] (0.1,0.15] (0.15,0.2] (0.2,0.25] (0.25,0.3]
(0.3,0.35]
        68         87         85         72         65         73
74
(0.35,0.4] (0.4,0.45] (0.45,0.5] (0.5,0.55] (0.55,0.6] (0.6,0.65]
(0.65,0.7]
        72         77         78         75         74         83
93
(0.7,0.75] (0.75,0.8] (0.8,0.85] (0.85,0.9] (0.9,0.95]   (0.95,1]
        89         75         67         59         56        208
> test.df <- as.data.frame (test.tab)
> test.df
         Var1 Freq
1    (0,0.05]   68
2  (0.05,0.1]   87
3  (0.1,0.15]   85
4  (0.15,0.2]   72
5  (0.2,0.25]   65
6  (0.25,0.3]   73
7  (0.3,0.35]   74
....


I would prefer a dataframe labelled like this.
0.05    68
0.1      87


How to do?
Thanks
Wim


> dput (test.tab)
structure(c(68L, 87L, 85L, 72L, 65L, 73L, 74L, 72L, 77L, 78L,
75L, 74L, 83L, 93L, 89L, 75L, 67L, 59L, 56L, 208L), .Dim = 20L, .Dimnames =
structure(list(
    c("(0,0.05]", "(0.05,0.1]", "(0.1,0.15]", "(0.15,0.2]", "(0.2,0.25]",
    "(0.25,0.3]", "(0.3,0.35]", "(0.35,0.4]", "(0.4,0.45]", "(0.45,0.5]",
    "(0.5,0.55]", "(0.55,0.6]", "(0.6,0.65]", "(0.65,0.7]", "(0.7,0.75]",
    "(0.75,0.8]", "(0.8,0.85]", "(0.85,0.9]", "(0.9,0.95]", "(0.95,1]"
    )), .Names = ""), class = "table")

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list