From hpages at fhcrc.org  Tue Mar 27 05:48:33 2007
From: hpages at fhcrc.org (hpages at fhcrc.org)
Date: Mon, 26 Mar 2007 20:48:33 -0700
Subject: [Rd] Unexpected result of as.character() and unlist() applied to a
	data frame
Message-ID: <1174967313.460894116113f@webmail.fhcrc.org>

Hi,

> dd <- data.frame(A=c("b","c","a"), B=3:1)
> dd
  A B
1 b 3
2 c 2
3 a 1
> unlist(dd)
A1 A2 A3 B1 B2 B3
 2  3  1  3  2  1

Someone else might get something different. It all depends on the
values of its 'stringsAsFactors' option:

> dd2 <- data.frame(A=c("b","c","a"), B=3:1, stringsAsFactors=FALSE)
> dd2
  A B
1 b 3
2 c 2
3 a 1
> unlist(dd2)
 A1  A2  A3  B1  B2  B3
"b" "c" "a" "3" "2" "1"

Same thing with as.character:

> as.character(dd)
[1] "c(2, 3, 1)" "c(3, 2, 1)"
> as.character(dd2)
[1] "c(\"b\", \"c\", \"a\")" "c(3, 2, 1)"

Bug or "feature"?

Note that as.character applied directly on dd$A doesn't
have this "feature":

> as.character(dd$A)
[1] "b" "c" "a"
> as.character(dd2$A)
[1] "b" "c" "a"

Cheers,
H.



[Rd] Unexpected result of as.character() and unlist() applied to a data frame
hpages at fhcrc.org