[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# R- count number occurrences in a txt file

Posted on 2013-12-19
Medium Priority
538 Views
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
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
• 3
• 2

LVL 37

Expert Comment

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

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

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

TommySzalapski earned 2000 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

ID: 39729541
Awesome thank you Tommy
0

## Featured Post

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress
###### Suggested Courses
Course of the Month13 days, 10 hours left to enroll