[R] Reshaping a table

arun
2013-04-08T17:48:46Z

The data frame looks okay.
str(dat1)
#'data.frame':??? 2 obs. of? 3 variables:
# $ X : num? 0.1 0.2
# $ Y1: int? 3 2
# $ Y2: int? 2 1

dat1<- structure(list(X = c(0.1, 0.2), Y1 = c(3L, 2L), Y2 = c(2L, 1L
)), .Names = c("X", "Y1", "Y2"), class =
"data.frame", row.names = c(NA,
-2L))
as.table(dat1)
#Error in as.table.default(dat1) : cannot coerce to a table



Tried with another data.frame
df1<- read.table(text="
mydate min_temp
31032013? 12
01042013 8
02042013 -999
",sep="",header=TRUE,colClasses=c("character","integer"))

as.table(df1)
#Error in as.table.default(df1) : cannot coerce to a table
I am not sure about the problem.
A.K.



----- Original Message -----
From: Bert Gunter <gunter.berton at gene.com>
To: arun <smartpink111 at yahoo.com>
Cc: "dcarlson at tamu.edu" <dcarlson at tamu.edu>
Sent: Monday, April 8, 2013 1:33 PM
Subject: Re: [R] Reshaping a table

Thanks.

Here's the relevant portion of the as.table Help file:

...???
one or more objects which can be interpreted as factors (including
character strings), or a list (or data frame) whose components can be
so interpreted. (For as.table and as.data.frame, arguments passed to
specific methods.)


So the Help file appears to be incorrect. Any idea what the problem
is? Should this be reported? Or is it something peculiar about this
data frame?

-- Bert


On Mon, Apr 8, 2013 at 10:10 AM, arun <smartpink111 at yahoo.com>
wrote:
> as.table(dat1)
> Error in as.table.default(dat1) : cannot coerce to a table
>? as.table(data.frame(dat1[,2:3],row.names=dat1[,1]))
> Error in as.table.default(data.frame(dat1[, 2:3], row.names = dat1[, 1])) :
>?  cannot coerce to a table
>
>
>? sessionInfo()
> R version 3.0.0 (2013-04-03)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>
> as.table(as.matrix(dat1))
> #? ? X? Y1? Y2
> #A 0.1 3.0 2.0
> #B 0.2 2.0 1.0
>
>
>
> A.K.
>
>
> ----- Original Message -----
> From: Bert Gunter <gunter.berton at gene.com>
> To: dcarlson at tamu.edu
> Cc: arun <smartpink111 at yahoo.com>; IOANNA <ii54250 at
msn.com>
> Sent: Monday, April 8, 2013 12:49 PM
> Subject: Re: [R] Reshaping a table
>
> David (et.al.):
>
> Just wanted to point out that the
> (as.table(as.matrix(data.frame(...)))) construction is not needed:
> as.table() works directly on the data frame, according to ?as.table
> anyway.
>
> Obviously, a pretty minor comment -- my sense of code aesthetics was
> just tweaked by the complex construction.
>
> If I'm wrong about this or I missed something essential, please let me
> know -- and accept my apology for _this_ annoyance.
>
> Cheers,
> Bert
>
> On Mon, Apr 8, 2013 at 9:16 AM, David L Carlson <dcarlson at
tamu.edu> wrote:
>> If you are starting with a table rather than a data frame, try this
>>
>>> # Convert Arun's data.frame, dat1, to a table, dat2
>>> dat2 <- as.table(as.matrix(data.frame(dat1[,2:3],
row.names=dat1[,1])))
>>> # convert dat2 to form requested
>>> dat3 <- data.frame(dat2)
>>> dat4 <- dat3[rep(1:nrow(dat3), dat3$Freq),1:2]
>>> colnames(dat4) <- c("X", "Y")
>>> rownames(dat4) <- NULL
>>> dat4
>>? ?  X? Y
>> 1 0.1 Y1
>> 2 0.1 Y1
>> 3 0.1 Y1
>> 4 0.2 Y1
>> 5 0.2 Y1
>> 6 0.1 Y2
>> 7 0.1 Y2
>> 8 0.2 Y2
>>
>> X and Y are factors. If you want them to be character vectors add:
>>
>>> dat4 <- data.frame(sapply(dat4, as.character),
stringsAsFactors=FALSE)
>>
>> ----------------------------------------------
>> David L Carlson
>> Associate Professor of Anthropology
>> Texas A&M University
>> College Station, TX 77843-4352
>>
>>
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>>> project.org] On Behalf Of arun
>>> Sent: Monday, April 08, 2013 7:41 AM
>>> To: IOANNA
>>> Cc: R help
>>> Subject: Re: [R] Reshaping a table
>>>
>>> Hi,
>>> Try this:
>>> dat1<-read.table(text="
>>> X? ? ? Y1? ? Y2
>>>
>>> 0.1? 3? ? ? 2
>>>
>>> 0.2? 2? ? ? 1
>>> ",sep="",header=TRUE)
>>>
>>>
>>>?
res<-do.call(rbind,lapply(split(dat1,seq_len(nrow(dat1))),function(x)
>>> {Y=rep(colnames(x)[-1],x[-1]); X=rep(x[,1],length(Y));
>>> data.frame(X,Y,stringsAsFactors=FALSE)}))
>>>? row.names(res)<- 1:nrow(res)
>>>? res
>>> #? ? X? Y
>>> #1 0.1 Y1
>>> #2 0.1 Y1
>>> #3 0.1 Y1
>>> #4 0.1 Y2
>>> #5 0.1 Y2
>>> #6 0.2 Y1
>>> #7 0.2 Y1
>>> #8 0.2 Y2
>>> A.K.
>>>
>>>
>>>
>>> ----- Original Message -----
>>> From: IOANNA <ii54250 at msn.com>
>>> To: 'r-help-r-project.org' <r-help at r-project.org>
>>> Cc:
>>> Sent: Monday, April 8, 2013 8:09 AM
>>> Subject: [R] Reshaping a table
>>>
>>> Hello all,
>>>
>>>
>>>
>>> I have data in the form of a table:
>>>
>>>
>>>
>>> X? ? ? Y1? ? Y2
>>>
>>> 0.1?  3? ? ?  2
>>>
>>> 0.2?  2? ? ?  1
>>>
>>>
>>>
>>> And I would like to transform in the form:
>>>
>>>
>>>
>>> X? ?  Y
>>>
>>> 0.1?  Y1
>>>
>>> 0.1?  Y1
>>>
>>> 0.1?  Y1
>>>
>>> 0.1?  Y2
>>>
>>> 0.1?  Y2
>>>
>>> 0.2? Y1
>>>
>>> 0.2? Y1
>>>
>>> 0.2? Y2
>>>
>>>
>>>
>>> Any ideas how?
>>>
>>>
>>>
>>> Thanks in advance,
>>>
>>> IOanna
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>? ?  [[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.
>>>
>>>
>>> ______________________________________________
>>> 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.
>>
>> ______________________________________________
>> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
>
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374