-3

I have data that illustrates hurricane tracks crossing through a series of "gates". How would I code it to output the GateID, and the count of times that each GateID occurs in the total data frame?

track_id    day hour    month   year    rate    gate_id pres_inter  vmax_inter
9   10  0   7   1   9.6451E-06  2   97809   23.545
9   10  0   7   1   9.6451E-06  17  100170  13.843
10  3   6   7   1   9.6451E-06  2   96662   31.568
13  22  12  8   1   9.6451E-06  1   94449   48.466
13  22  12  8   1   9.6451E-06  17  96749   30.55
16  13  0   8   1   9.6451E-06  4   98702   19.205
16  13  0   8   1   9.6451E-06  16  98585   18.143
19  27  6   9   1   9.6451E-06  9   98838   20.053

header <- read.table(fname_in, nrows=1)
track <- read.table(fname_in, sep=',', skip=1)
colnames(track) <- c("ID", "day", "month", "year", "hour", "rate", "gate_id", "pres_inter", "vmax_inter")

I think I would like to count the occurrence of each gate_id, and also perhaps output the maximum wind per gate (vmax_inter), etc....

2
  • 3
    Please provide more details and a reproducible example. As much as some people like to believe it - we're not wizards and we can't read minds.
    – Dason
    Nov 19, 2012 at 21:23
  • A sample of your data would greatly help
    – Chargaff
    Nov 19, 2012 at 21:35

1 Answer 1

1

Totally reading your mind, since you provide nothing concrete to go on. But if GateID is one of your data frame columns, you can get the count for each unique GateID along with other parameters using count from package plyr.

install.packages("plyr")
library("plyr")
count(mydf, vars = "GateID")

See ?count after installing for further details.

For the 2nd part of your question, see ?aggregate and consider the formula interface. For example,

aggregate(gate_id ~ vmax_inter, data = mydf, FUN = max)

or something similar. By the way, you can combine your two read.table steps with 'read.csv`

5
  • I'm not the downvoter, but why not just use table(df$GateID)? Nov 19, 2012 at 21:43
  • table would be about the same, but I prefer count for it's format. I'm sure I'm down voted for answering an improperly posed question. Oh well. Nov 19, 2012 at 21:46
  • I would like to include two output columns after frequency that include the average vmax_inter per gate_id, and also the maximum vmax_inter per gate_id. Does that make sense?
    – kimmyjo221
    Nov 19, 2012 at 22:08
  • I have never seen or heard of read.csv. I am very new to R programming. Thanks!
    – kimmyjo221
    Nov 19, 2012 at 22:13
  • Yes, but you would have to write your own FUN to specify what you want and then use aggregate(..., FUN = myfun). I tried a quick example and it will need some tweaking, so instead let me suggest FUN = fivenum Do ?fivenum and you'll see what the 5 columns give you. Then you can subset the resulting data frame so it contains only the values you want. Example: res <- aggregate(weight ~ feed, data = chickwts, FUN = fivenum) Nov 19, 2012 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.