[R] Grouping distances

Rui Barradas ruipbarradas at sapo.pt
Mon Jun 11 09:18:47 CEST 2012


Hello,

It's easy to create a new column. Since you haven't said where nor the 
type of data structure you are using, I'll try to answer to both.
Suppose that 'x' s a matrix. Then

newcolumn <- newvalues
x2 <- cbind(x, newcolumn)  # new column added to x, result in x2

Suppose that 'y' is a data.frame. Then the same would do it, or

y$newcolumn <- newvalues

Now, I believe that the new values come from your function. If so, you 
must assign the function value to some variable outside the function.

htlindex <- HTL.index(...etc...)  # 'htlindex' is the 'newvalues' above


Two extra notes.
One, rowSums() does what your apply() instructions do.

Second, first you multiply then you divide, to give 'weights'. I think 
this is just an example, not the real function.

Hope this helps,

Rui Barradas

Em 11-06-2012 07:01, Jhope escreveu:
> Hi R-listers,
>
> I am trying to group my HTL data, this is a column of data of "Distances to
> the HTL" data = turtlehatch. I would like to create an Index of distances
> (0-5m, 6-10, 11-15, 16-20... up to 60). And then create a new file with this
> HTLIndex in a column.
>
> So far I have gotten this far:
>
> HTL.index <- function (values, weights=c(0, 5, 10, 15, 20, 25, 30, 35, 40,
> 45, 50, 55, 60)) {
> hope <-values * weights
> return (apply(hope, 1, sum)/apply(values, 1, sum))
> }
> write.csv(turtlehatch, "HTLIndex", row.names=FALSE)
> ----
>
> But I do not seem to be able to create a new column " in a new file.
>
> Please advise, Jean
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Grouping-distances-tp4632985.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list