我的数据框有500行,每行至少有12列。有些行有很多列。我想要做的是创建一个变量,其中包含每行最后一个填充单元格的内容。
dput(head(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]
}}
我希望“last”包含每行中最后一个填充的单元格,除非它显示“none”。从B栏开始。
即。在这种情况下
last
[1] kasdpa
[2] wads
道歉,如果这是一个不起眼的问题,但我正在自学编程。
答案 0 :(得分:3)
已编辑:错误的行和列
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"