From table to data.frame

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

From table to data.frame

Wim Kreinen
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: From table to data.frame

arun kirshna
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 <[hidden email]>
To: [hidden email]
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]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: From table to data.frame

arun kirshna
In reply to this post by Wim Kreinen
Hi,
set.seed(25)
 Z<-rnorm(50,0.5,1)
res<- as.data.frame(table(cut(Z,seq(0,1,by=0.05),labels=seq(0.05,1,by=0.05))) )


 head(res)
 # Var1 Freq
#1 0.05    1
#2  0.1    1
#3 0.15    0
#4  0.2    0
#5 0.25    0
#6  0.3    1
A.K.




----- Original Message -----
From: Wim Kreinen <[hidden email]>
To: [hidden email]
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]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: From table to data.frame

Rui Barradas
In reply to this post by Wim Kreinen
Hello,

try the following.

test.df$Var1 <- seq(0,1, by=0.05)[-1]
test.df


Hope this helps,

Rui Barradas

Em 24-01-2013 17:39, Wim Kreinen escreveu:

> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: From table to data.frame

Wim Kreinen
This idea is very simple and helpful. But one has to start with 0.05.
Thanks.

test.df$n<- seq(0.05,1, by=0.05)

2013/1/24 Rui Barradas <[hidden email]>

> Hello,
>
> try the following.
>
> test.df$Var1 <- seq(0,1, by=0.05)[-1]
> test.df
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 24-01-2013 17:39, Wim Kreinen escreveu:
>
>> 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]]
>>
>> ______________________________**________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.