Solved

R- count number occurrences in a txt file

Posted on 2013-12-19
5
519 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now