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
Solved

R- count number occurrences in a txt file

Posted on 2013-12-19
5
528 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
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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.
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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