0

I have a function that I want to use it but the inputs are from a text file.

Here is the Fun:

myfun <- function(latitude,longitude) {
   column =latitude*5
   row    =longitude*3
  return(c(column, row))
         }

Now I have a text file with information for my function.

cor=read.table("C:\\Data\\AMS.txt", sep="")
head(cor)
  V1       V2  V3     V4    V5   V6
1 lat      13 lon     2   Site:   As
2 lat      14 lon     3   Site:   Ad

Output needed for instance:

 lat       lon      column  row  site    
 13         2        ?       ?    As

I can do this manually but as I have many, it would be better to let R do it. Any hints are appreciated

sacvf
  • 2,463
  • 5
  • 36
  • 54
  • 1
    The best thing would be to define a function by new column for example `func = function(lat, long) lat+long` and then do something like `transform(cor, column=func(V2,V4))` – Colonel Beauvel May 12 '15 at 13:38
  • 1
    your function can return a list: `func = function(lat, long) list(lat+long,lat* long))` and: `transform(cor, newcol1=func(V2,V4)[[1]], newcol2=func(V2,V4)[[2]])` – Colonel Beauvel May 12 '15 at 13:47
  • it's better to use a vecotrized operation instead of a function ... if you really need a function row per row, consider using `apply`. The problem is your minimal example can be vectorized so what you're really asking is misleading ... – Colonel Beauvel May 12 '15 at 14:19
  • I've had another look at your function and for each of `column` and `row` it returns a vector of 5 items. Is this correct? – Phil May 13 '15 at 11:27

2 Answers2

2

Try:

data.frame(lat=DF$V2, lon=DF$V4, column=DF$V2*5, row=DF$V4*3, site=DF$V6)

No need for cleaning.

Data

DF <- read.table(text="  V1       V2  V3     V4    V5   V6
1 lat      13 lon     2   Site:   As
2 lat      14 lon     3   Site:   Ad", header=T)

> DF1 <- data.frame(lat=DF$V2, lon=DF$V4, column=DF$V2*5, row=DF$V4*3, site=DF$V6)
> DF1
  lat lon column row site
1  13   2     65   6   As
2  14   3     70   9   Ad
Community
  • 1
  • 1
Pierre L
  • 28,203
  • 6
  • 47
  • 69
1

Unless you need to use a function I would use a vectorised solution. First, I'd tidy up your data frame:

cor <- read.table("C:\\Data\\AMS.txt", sep="")  # note <- not =
require("dplyr")
cor <- select(cor, -V1)
cor <- select(cor, -V3)
cor <- select(cor, -V5)
colnames(cor) <- c("lat", "long", "site")

Then I'd simply create a new variable for column and row:

cor$column <- cor$lat * 5
cor$row    <- cor$long * 3

Yielding:

cor
#   lat long site column row
# 1  13    2   As     65   6
# 2  14    3   Ad     70   9

EDIT: Based on your comments and edited post you clearly have a more complex function, which I've attempted to vectorise below. The output is for a vector of 5 items for each of columns and row, so hopefully that's your expected behaviour.

kR  = 6371.228  # recommend constants start with 'k'
kC  = 25.067525
kNc = 1383
kNl = 586
kR0 = (kNc - 1) / 2
kS0 = (kNl - 1) / 2
kDeg2rad_cte = pi/180

cor$lamda <- cor$lon * kDeg2rad_cte
cor$phi   <- cor$lat * kDeg2rad_cte

column <- round(kR0 + (kR / kC) %*% cor$lamda * cos(pi / 6)) + 1
row    <- 586 - round(kS0 - (kR / kC) %*% sin(cor$phi) / cos(pi / 6))

column <- seq(min(column), max(column), by=1) 
row    <- seq(min(row), max(row), by=1) 

column
# [1] 700 701 702 703 704

row
# [1] 360 361 362 363 364
Phil
  • 4,344
  • 2
  • 23
  • 33
  • Can your actual function be vectorised? Can you post it? – Phil May 12 '15 at 13:55
  • I'm afraid I don't have any experience with matrix multiplication so I can't help further. It probably can be vectorised, but if you want to use a function check out `apply(cor, margin = 1, function(...)` to apply a calculation over the rows in a data frame. – Phil May 12 '15 at 15:19