3

500 行で、1 行あたり少なくとも 12 列のデータ フレームがあります。一部の行には、より多くの列があります。私がやりたいことは、行ごとに最後に入力されたセルの内容を含む変数を作成することです。

dput (ヘッド (calls_with_overlap, 10))

    A      B        C       D
1. aknasd kasdknsd kasdkas kasdpa
2. asdhad none
3. apihsd piahsdas ashpidapishd phasd askdn wads
4. kasdas none

私がこれまでに試したことは次のとおりです。

if (calls_with_overlap$Overlap[10] = "none") 
{
Ending <- TRUE
} 
else (Ending <- FALSE)
 if (Ending == FALSE) 
   {
  for (q in 1:1024) #max is 1024 columns. perhaps not best way for number of columns.
  {
   EndingCell <- c(10 + q)
   EndingCellcontents <- calls_with_overlap[h,EndingCell] # h is from an outer loop, going through each row. 
     if (EndingCellcontents != ""){ 
       EndingCellcontents[q] <-    EndingCellcontents}
       last  <-  length(EndingCellcontents)
       last<-  EndingCellcontents[last]
      }}

「なし」と表示されていない限り、「最後」に各行の最後に入力されたセルを含めたいと思います。B列から。

つまり、この場合

last
[1] kasdpa
[2] wads

これが初心者の質問である場合は申し訳ありませんが、私はゼロからプログラミングを教えています。

4

1 に答える 1

4

編集済み: 間違った行と列

df <- data.frame(A=c("r","","z"),B=c("w","",""))
df
  A B
1 r w
2    
3 z  
v <- apply(df, 1, function(x) x[rev(which((x!="")==TRUE))[1]])
[1] "w" NA  "z"
v[!is.na(v)]
[1] "w" "z"
于 2012-06-29T14:01:32.567 に答える