-1

I have a pretty standard table, usually when I do something like this to read in a column from the table it works fine, but for some reason this time it didn't.

x <- dataFile$columnName

Here is what I get:

[1] 61 71 83 55 44 78 57 46 41 36 45 48 38 33 54 62 60 44 70 49 57 86 41 71 59 52 51 62 45 43
54 Levels:  33-87    36-81    38-79    41-70    41-85    43-47    44-51    44-62    45-51   ... 43

I just want the top part, without all the levels in it.

-----------

Edit (in preparation for this being closed): rather than leaving what is now misleading information in the comments (in case this shows up in someone's search), the top part is just a set of factor levels. If you just wanted those values (as text) you would use: as.character(x). If you wanted the characters before the minus sign if one were present, you could use:

  as.numeric( sub("\\-.+$", "", as.character(x) ) )

Using as.numeric(x) would generally return values from 1 to 54 and is meaningless unless you only want an index.

Community
  • 1
  • 1
user2963379
  • 155
  • 1
  • 12

1 Answers1

0

from ?read.table

stringsAsFactors
logical: should character vectors be converted to factors? Note that this is overridden by as.is and colClasses, both of which allow finer control.

So set stringAsFactors = FALSE in the read.table command:

data <- read.table("C:/rk/R/problem.txt", sep="\t", fill = FALSE, stringsAsFactors = FALSE)

output for one column:

data$V21
 [1] "avg "   " 45.0 " " 45.5 " " 55.7 " " 58.2 " " 60.0 " " 60.2 " " 58.3 " " 58.4 " " 57.3 " " 55.7 " " 53.4 " " 52.5 " " 52.7 " " 53.4 " " 51.9 "
[17] " 52.6 " " 53.1 " " 53.1 " " 53.5 " " 52.4 " " 52.3 " " 52.0 " " 52.1 " " 51.9 " " 51.7 " " 52.1 " " 52.1 " " 52.4 " " 51.8 " " 51.6 " " 51.0" 

There are no levels when stringAsFactors is set to FALSE.

vamosrafa
  • 685
  • 5
  • 11
  • 35