Reading numbers as strings

I am new to R programming and I want to read a text file in R.

One of the columns, it can be said that column 7 is numeric, and each number is an identifier that I want R to read numbers as if they were strings. And count the number of times each identifier appears in the file (so that later I can assign the frequency of each ID to this identifier for the last use) I tried

mydata<-(read.table(filename.txt)) ID=mydata[7] freq=table(ID) 

This works, but it accepts identifiers as numbers. Now i tried

 freq=table(as.character(ID)) 

But then it takes the entire column id as only one row and from

 summary(freq) 

I get

 Number of cases in table: 1 Number of factors: 1 
+8
file r formal-languages
source share
3 answers

While reading data in your data frame from a text file, you can specify the type of each column using the colClasses argument. The following is the file on 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 

since you can see that R no longer treats zipcodes as numbers, but as factors. In your case, you need to specify the class of the first 6 columns, and then select factor as your seventh. Therefore, if the first 6 columns are numeric, it should be something like this colClasses = c(rep("numeric",6),"factor") .

+9
source share

without as.character your table should work correctly (i.e. freq <- table(ID) ), quoting from ?table , your input could be:

one or more objects that can be interpreted as factors (including character strings) or a list (or data frame) whose components can be interpreted in this way. (For as.table and as.data.frame, arguments were passed specific methods.)

+4
source share

I think you missed a comma in your data frame.

 mydata<-(read.table(filename.txt)) ID=mydata[,7] #added comma freq=table(as.character(ID)) 
+2
source share

All Articles