由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 【R】关于R的variable type
相关主题
用R出现怪问题。R 小问题
用R灌水的终极利器。有技巧得用R才能发挥它的威力
data clustering by vector correlation distance一个数据文件里边某些cell 包含字符“,”的问题
求助:Import .sas7bdat to R大家抱怨下R的问题吧
R: 怎么读入某个field中含有comma的文件?求教一个R 的简单问题!
问 R 的 data type 问题The Data and Story Library
How can I do this in R?SAS不能导入EXCEL2003的数据!
what is factor for in R?Help:import excel file into sas
相关话题的讨论汇总
话题: factor话题: variable话题: character话题: data话题: 空格
进入Statistics版参与讨论
1 (共1页)
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
8
这么晚了不睡觉,熬夜搞科研吗。
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")

1 (共1页)
进入Statistics版参与讨论
相关主题
Help:import excel file into sasR: 怎么读入某个field中含有comma的文件?
what is the problem?问 R 的 data type 问题
急问:怎么用SAS读这个Excel文件啊How can I do this in R?
請教許多data 快速import and transpose的方法what is factor for in R?
用R出现怪问题。R 小问题
用R灌水的终极利器。有技巧得用R才能发挥它的威力
data clustering by vector correlation distance一个数据文件里边某些cell 包含字符“,”的问题
求助:Import .sas7bdat to R大家抱怨下R的问题吧
相关话题的讨论汇总
话题: factor话题: variable话题: character话题: data话题: 空格