1

時間と位置のデータを含み、行ごとに1つの値を取得する、データフレームMの334行すべてに関数を適用しようとしています。代わりに、各行の334個の値のリストを取得します。同じ行の変数の値から計算された行ごとに1つの値を簡単に取得するにはどうすればよいですか?

これらは、データフレームMのヘッドとテールです。

d mo    y   lat  long
 5  6 2007    NA    NA
 6  6 2007    NA    NA
 7  6 2007    NA    NA
 8  6 2007 26.89 15.53
 9  6 2007 28.00 15.73
10  6 2007 22.41 14.93
...
26  4 2008 23.86 14.05
27  4 2008 24.12 14.34
28  4 2008 27.75 12.87
29  4 2008 27.28 10.91
30  4 2008 24.17 14.44
1  5 2008    NA    NA

私のコード:

f1 = function(x){
         options(latitude= M$lat, longitude= M$long); 
         as.lt(moon.rst(jday = jd(M$y,M$mo,M$d)))
}
M$rs <- apply(M, 1, f1)
4

1 に答える 1

2
f1  <- function(d, mo, y, lat, long){
  options(latitude = lat, longitude = long)
  as.lt(moon.rst(jday = jd(y, mo, d)))
}
data$rs <- do.call(f1, data)
Warning message:
In if (year < 0) c = floor((365.25 * year) - 0.75) else c = floor(365.25 *  :
  the condition has length > 1 and only the first element will be used
data
   d mo    y   lat  long     rs.rise  rs.transit      rs.set
1  8  6 2007 26.89 15.53  0h 24m 30s  5h 42m  2s 11h 42m 13s
2  9  6 2007 28.00 15.73  1h  4m 42s  6h 34m  5s 12h 46m 30s
3 10  6 2007 22.41 14.93  1h 54m 38s  7h 31m  8s 13h 53m 59s

jd()ベクトル(この場合)を引数として渡しているため、関数から警告がありますがyear、それにもかかわらず、それが必要な結果であることを願っています。

編集:警告なしの別のバージョン、を使用しapplyていますが、インデックスを使用しており、私にとってdo.callはより高速であるように見えました。

f1 <- function(M){
  options(latitude= M[4], longitude= M[5]); 
  as.lt(moon.rst(jday = jd(M[3],M[2],M[1])))
}
apply(data[,c('d','mo','y','lat','long')], 1, f1)
[[1]]
                  rise     transit         set
2005-06-08  6h  2m 30s 13h  8m  3s 20h 12m 51s

[[2]]
                  rise     transit         set
2005-06-09  6h 55m 34s 14h  1m 26s 21h  4m 44s

[[3]]
                  rise     transit         set
2005-06-10  8h  7m 41s 14h 57m  7s 21h 43m 36s
于 2012-07-09T18:25:21.503 に答える