a***r 发帖数: 420 | 1 这个问题以及很多跟factor相关的问题,都真的搞得我很痛苦。。。
我以为我明白factor,numeric,character的区别,在字面上是很容易理解(?),可
是一自己写code,就会被无数莫名的问题折磨,(尤其跟factor有关的。。。)然后跑
去找教程,看了教程以上循环重复。。。
比如,我读入了一个variable用tab隔开,但是varible中间有空格的datafile,于是用
的:
data <-read.table("data.txt",sep="\t",strip.white=TRUE)
数据第一行如下:
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
1 139930032001 139930032001 0 0 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
从第七列开始,都是1 1;2 2;这样的格式,中间有一个空格
然后R认为他是factor
于是我写
data[,(6+1):(6+n)] <-as.character(data[ |
D*******a 发帖数: 207 | 2 你这是plink的genotype格式吧。
类似“1空格1”,“2空格2”的值,应该是character类型的;R把character类型的缺
省读成factor的。好像你是想要character类型,不想要factor类型。可以在read.
table里面加个参数:stringsAsFactors=FALSE,就可以了。 |
a****r 发帖数: 1486 | 3 试试直接用sep=""
【在 a***r 的大作中提到】 : 这个问题以及很多跟factor相关的问题,都真的搞得我很痛苦。。。 : 我以为我明白factor,numeric,character的区别,在字面上是很容易理解(?),可 : 是一自己写code,就会被无数莫名的问题折磨,(尤其跟factor有关的。。。)然后跑 : 去找教程,看了教程以上循环重复。。。 : 比如,我读入了一个variable用tab隔开,但是varible中间有空格的datafile,于是用 : 的: : data <-read.table("data.txt",sep="\t",strip.white=TRUE) : 数据第一行如下: : V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 : 1 139930032001 139930032001 0 0 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
|
D******n 发帖数: 2836 | 4 if he wants to do it his way,
data[,]=as.character(as.matrix(data[,]))
【在 D*******a 的大作中提到】 : 你这是plink的genotype格式吧。 : 类似“1空格1”,“2空格2”的值,应该是character类型的;R把character类型的缺 : 省读成factor的。好像你是想要character类型,不想要factor类型。可以在read. : table里面加个参数:stringsAsFactors=FALSE,就可以了。
|
a***r 发帖数: 420 | 5 是啊,用过的人一看就知道:)
我只是想把问题改得general一点,就没特定是啥文件来的
谢谢!
其实我的目的很简单,就是把“1 1"换成additive model的dosage
今天后来发现plink自己可以用--recodeA 直接生成到。。。
【在 D*******a 的大作中提到】 : 你这是plink的genotype格式吧。 : 类似“1空格1”,“2空格2”的值,应该是character类型的;R把character类型的缺 : 省读成factor的。好像你是想要character类型,不想要factor类型。可以在read. : table里面加个参数:stringsAsFactors=FALSE,就可以了。
|
a***r 发帖数: 420 | 6 这个ms不行,我是要把1 1变成一个值,而不是分成两个值~
【在 a****r 的大作中提到】 : 试试直接用sep=""
|
a***r 发帖数: 420 | 7 谢谢~!
【在 D******n 的大作中提到】 : if he wants to do it his way, : data[,]=as.character(as.matrix(data[,]))
|
D*******a 发帖数: 207 | |
s*r 发帖数: 2757 | 9 to all statgen comrades:
please read manuals, including 'help(read.table)' from the begining to end ,
and learn perl if possible. |
d*******1 发帖数: 854 | 10 为什么一定要学perl呢,难道用R操作data不行吗?因为作一个系统还是尽可能在一个
平台里完成吧
,
【在 s*r 的大作中提到】 : to all statgen comrades: : please read manuals, including 'help(read.table)' from the begining to end , : and learn perl if possible.
|
s*r 发帖数: 2757 | 11 the unit of operation in R is a column (vector), if you want to do element-
wise work, perl, or other general purposes languages are better. |
a***r 发帖数: 420 | 12 确实,这个问题确实是我读manual读得不够仔细。。。
不过其他有的时候跟factor有关的东西,也会搞得我比较郁闷:(
,
【在 s*r 的大作中提到】 : to all statgen comrades: : please read manuals, including 'help(read.table)' from the begining to end , : and learn perl if possible.
|
q****o 发帖数: 37 | 13 read.table is "devil", which will yield a data.frame. Thus it assumes all
the string variables are factor.
it's safe to predefine the classes you want to read if possible such as
dat<-read.table("test.txt",colClasses=c(rep('numeric',2),rep('character',4))
,header=T,sep="\t") |
a***r 发帖数: 420 | 14 wow,you suggested exactly what my supervisor said:)
))
【在 q****o 的大作中提到】 : read.table is "devil", which will yield a data.frame. Thus it assumes all : the string variables are factor. : it's safe to predefine the classes you want to read if possible such as : dat<-read.table("test.txt",colClasses=c(rep('numeric',2),rep('character',4)) : ,header=T,sep="\t")
|