reading txt file
1
0
Entering edit mode
7.3 years ago
niutster ▴ 110

Hi, I have read a text file by read.delim("Chr.txt), Chr file contains numbers but when i want to get a number and compare , i dont get correct answer:

my_data <- read.delim("Chr.txt")
> my_data[1,1]
[1] 2
Levels: 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 X Y
> my_data[1,1] <3
[1] NA
Warning message:
In Ops.factor(my_data[1, 1], 3) : ‘<’ not meaningful for factors
R txt file • 1.6k views
ADD COMMENT
3
Entering edit mode
7.3 years ago

This is because my_data[1,1] -> 2 looks like a number but it is not, it's a factor, a label. R read column 1 as factor because you have X and Y that are not numeric.

To convert to numeric use as.numeric(my_data[1,1]). However, I would consider whether it is a meaningful operation to treat chromosome names as numbers.

ADD COMMENT
0
Entering edit mode
d=as.numeric(my_data[1,1])
> my_data[1,1]
[1] 2
Levels: 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 3 4 5 6 7 8 9 X Y
> d
[1] 12
ADD REPLY
0
Entering edit mode

It does not work correctly.

ADD REPLY
0
Entering edit mode

I can reproduce the problem in your original question but not the one here in the comment:

my_data<- data.frame(x= c(2, 3, 1, 'x', 'y'))
my_data[1,1] < 3 # Give NA and Warning

d<- as.numeric(my_data[1,1]) # -> d= 2

d < 3 # -> TRUE
ADD REPLY

Login before adding your answer.

Traffic: 1700 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6