以字符串形式读取数字 [英] Reading numbers as strings
问题描述
我是R编程的新手,我想在R中读取文本文件.
I am new at R programming and I want to read a text file in R.
其中一列,可以说第7列是数字,并且每个数字都代表一个ID,我希望R像将它们当作字符串一样读取它们.并计算每个ID在文件中出现的次数(这样以后我就可以将每个ID的频率分配给给定的ID,以供以后使用) 我尝试过
One of the columns, lets say column 7 is numeric and each number represent an ID I want R to read the numbers as if they were strings. And count the number of times each ID appear in the file (such that later I can assign the frequency of each ID to the given ID for latter use) I have tried
mydata<-(read.table(filename.txt))
ID=mydata[7]
freq=table(ID)
此方法有效,但是将ID作为数字.现在我尝试了
This works but it takes the IDs as numbers. Now I have tried
freq=table(as.character(ID))
但是随后它将整个列ID仅作为一个字符串,并且来自
But then it takes the whole column ID as only one string and from
summary(freq)
我知道
Number of cases in table: 1
Number of factors: 1
推荐答案
从文本文件将数据读取到数据框中时,可以使用colClasses
参数指定每列的类型.请参阅下面的文件,我的计算机中有此文件:
At the time of reading the data into your data frame from the text file you can specify the type of each column using the colClasses
argument. See below a file have in my computer:
> head(read.csv("R/Data/ZipcodeCount.csv"))
X zipcode stateabb countyno countyname
1 1 401 NY 119 WESTCHESTER
2 391 501 NY 103 SUFFOLK
3 392 544 NY 103 SUFFOLK
4 393 601 PR 1 ADJUNTAS
5 630 602 PR 3 AGUADA
6 957 603 PR 5 AGUADILLA
> head(read.csv("R/Data/ZipcodeCount.csv",colClasses=c(rep("factor",5))))
X zipcode stateabb countyno countyname
1 1 00401 NY 119 WESTCHESTER
2 391 00501 NY 103 SUFFOLK
3 392 00544 NY 103 SUFFOLK
4 393 00601 PR 001 ADJUNTAS
5 630 00602 PR 003 AGUADA
6 957 00603 PR 005 AGUADILLA
> zip<-read.csv("R/Data/ZipcodeCount.csv",colClasses=c(rep("factor",5)))
> str(zip)
'data.frame': 53424 obs. of 5 variables:
$ X : Factor w/ 53424 levels "1","10000081",..: 1 36316 36333 36346 43638 52311 19581 23775 26481 26858 ...
$ zipcode : Factor w/ 41174 levels "00401","00501",..: 1 2 3 4 5 6 6 7 8 9 ...
$ stateabb : Factor w/ 60 levels ""," ","AK","AL",..: 41 41 41 46 46 46 46 46 46 46 ...
$ countyno : Factor w/ 380 levels "","000","001",..: 106 95 95 3 5 7 5 7 7 9 ...
$ countyname: Factor w/ 1925 levels "","ABBEVILLE",..: 1844 1662 1662 9 10 11 10 11 11 12 ...
> head(table(zip[,"zipcode"]))
00401 00501 00544 00601 00602 00603
1 1 1 1 1 2
如您所见,R不再将邮政编码视为数字,而是作为因素.在您的情况下,您需要指定前6列的类,然后选择factor
作为第七列.因此,如果前6列为数字,则应为colClasses = c(rep("numeric",6),"factor")
.
as you can see R is no longer treating zipcodes as numbers but as factors. In your case you need to specify the class of the first 6 columns and then choose factor
as your seventh. So if the first 6 columns are numeric it should be something like this colClasses = c(rep("numeric",6),"factor")
.
这篇关于以字符串形式读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!