?
Solved

R- count number occurrences in a txt file

Posted on 2013-12-19
5
Medium Priority
?
536 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 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

by:jay_eire
ID: 39729541
Awesome thank you Tommy
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Introduction to Processes
Suggested Courses

764 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