manish kumar manish kumar - 1 month ago 8
R Question

First non empty value in Column of data frame in R

I have one data frame which has column header but position of coulmn header is not fixed so can i read non empty value in 1st column to get the index of header to process the file.

mydata.txt

test 34 45
rt 45 56
tet3 67 56
Col1 Col2 Col3 Col4 Col5
45 45 23 56 12
34 45 67 65 32
45 67 78 90 54
56 43 32 12 45


mydata = read.table("mydata.txt")
mydata[,1] #how to find first non blank value in first column?


In order simplify the about pblm:

df<-c("","","",34,23,45)

how to find fiest non blank value in df

Answer

Trying to answer your "simplified" problem:

df <- c("", "", "", 34, 23, 45)

The purrr package provides such functions with detect() and detect_index():

install.packages("purrr", repos = "https://cloud.r-project.org")
library(purrr)
detect_index(df, function(x) x != "")