Solved

R- count number occurrences in a txt file

Posted on 2013-12-19
5
533 Views
Last Modified: 2013-12-19
Hello ,
I have a large txt file that has 1000s of rows with numbers that's range from -8 to 15. I've imported it into r studio as survey_results and it has variable of V1
Example from text file:
V1
6
-7
10
-3
-1
-1

I'm looking for R to count the number of times each number appears in the file so I would have something like this.
-7 300 times
-6 10 times
-5 139 times
-4 10 times
is this possible to do in R?
0
Comment
Question by:jay_eire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39728801
Yes. Those are referred to in R as frequency tables and you can generate one by simply calling the table() function.

occurrences <- table(V1) # calculate the frequencies
occurrences # print them
0
 
LVL 7

Author Comment

by:jay_eire
ID: 39728851
Occurrences <- table(survey_results$V1, FUN=SUM)
Print(occurrences)
Thanks for reply, I've tried the following but I'm getting an error saying all arguments must be same.

I'm new to R here how do I Calculate the frequencies? Could you provided an example?
0
 
LVL 7

Author Comment

by:jay_eire
ID: 39728879
Occurrences <-table(survey_results)
as.data.frame(table(survey_results))

Retuns the counts  Can I order the table by -8 to 15 so it looks like this?

-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 39729455
Occurrences <-sort(table(survey_results)[,1]
This should sort it by the column name.
If you also want it transposed, use
Occurrences <-t(sort(table(survey_results)[,1])
0
 
LVL 7

Author Comment

by:jay_eire
ID: 39729541
Awesome thank you Tommy
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Make the most of your online learning experience.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question