R从功能导出数据框?

时间:2015-05-13 08:01:06

标签: r

我已经创建了一个在手动运行时有效的功能,但是当它作为一个函数运行时它不会返回数据框。 数据框的名称是距离,每次计算子距离时都会输入,参见“### Save to Distances Data.Frame ###”部分代码。

    CloseGPS <- function(lat, lon,namelon,namelat,df){
  #make data.frame Distances#
  Distances <- data.frame( "Main.Point" = integer(0), "Sub.Point" = integer(0), "Distance" = integer(0), stringsAsFactors = FALSE)
  P<-length(df$LongD)
  NRuns <- 0
  for(Q in 1:P){
    ### Core Point Loop ###
    NRuns<- NRuns + 1
    nRuns <- 0
    lat1<- df[NRuns, namelat]
    lon1<- df[NRuns, namelon]
    cat("Point for Analysis", NRuns, '\n')
    cat("Lat:", lat1, '\n')
    cat("Long:", lon1, '\n')

    ### Distance Loop ###
    for (i in 1:P ) {
      nRuns<- nRuns + 1
      lat2<- df[nRuns, namelat]
      lon2<- df[nRuns, namelon]

      ###############
      R<-6378137
      radians <- 0.0174532925
      rlon1 <- lon1* radians
      rlat1 <- lat1* radians
      rlon2 <- lon2* radians
      rlat2 <- lat2* radians
      rdlon <- rlon2-rlon1
      rdlat <- rlat2-rlat1
      a <- ((sin((rdlat)/2))^2) + cos(rlat1) * cos(rlat2) * ((sin((rdlon)/2))^2)
      c <- 2 * atan2(sqrt(a), sqrt(1-a)) 
      d = c*R
      ###############

      # Print data #
      cat("- sub point:", nRuns," ---- distance", d, '\n')
      ### Save to Distances Data.Frame ###
      E<-nrow(Distances)+1
      Distances[E,"Sub.Point"]<-nRuns
      Distances[E,"Main.Point"]<-NRuns
      Distances[E,"Distance"]<-d

      ### End Distance Loop ###
    }

    ### End Core Point Loop ###
  }
}

感谢您的帮助!

0 个答案:

没有答案