[R] problem about using list element in for cycle

Tony Plate tplate at acm.org
Mon Oct 23 18:30:27 CEST 2006


Your problem is that you are using cat() on a factor.  Use 
as.character() or format() to convert the factor to character data, 
which cat will then print in the way you want.

 > x <- data.frame(L=letters[1:3])
 > x
   L
1 a
2 b
3 c
 > x$L
[1] a b c
Levels: a b c
 > cat(x$L, "\n")
1 2 3
 > cat(as.character(x$L), "\n")
a b c
 > cat(format(x$L), "\n")
a b c
 >


Hu Chen wrote:
> sorry, pressed "sent" by mistake.
> for example
> 
>>data <- read.csv("data.txt")
>>single
> 
>             V1      V2
> 1      YHR165C  CG8877
> 2      YJL130C CG18572
> 3      YDL171C  CG9674
> 4      YKR054C  CG7507
> 5      YDL140C  CG1554
> 6      YLR106C CG13185
> 7      YGL206C  CG9012
> 8      YNL262W  CG6768
> 9      YER172C  CG5931
> 
> 
>>typeof(data)
> 
> [1] "list"
> 
>>for (i in 1:nrow(data)){
> 
>      cat(data[i,1]
>    }
> 
> it'll not return things like "YHR165C" but number like 6,7,9..
> is this a new feature of list? how to turn off it.
> thanks
> 
> On 10/23/06, Hu Chen <chencheva at gmail.com> wrote:
> 
>>for example
>>data <- read.csv("data.txt")
>>typeof(data)
>>[1] "list"
>>for (i in 1:nrow(data)){
>>
>>
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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