Pau
Pau

Reputation: 125

how to get a particular row from a data frame with if else

I have a data frame (df) with ncol=1; nrow=2128.

this is the data structure:

208    INCL10
209    0.006882
210    INCL11
211    0.006780
212    INCL12
213    0.006590
214    INCL13
215    0.005693
216    INCL14
217    0.005225

So i want to know what is the value for INCL10 automatically (that is 0.006882). i have this script:

for(i in 1:length(df[,1])) {if (df[i,1]=="INCL10") {INCL10=df[i+1,1]}  else CALENDARDATE="none" }

but when i evaluate the sentence the result is "none"

what i am doing wrong?

Upvotes: 0

Views: 66

Answers (1)

symbiotic
symbiotic

Reputation: 373

You can use which to get the index of "INCL10" then add 1 to get the next row.

data <- data.frame(X = c("INCL10", ".006882", "INCL11", ".00678", "INCL12", "0.006590", "INCL13", "0.005693", "INCL14", "0.005225"))


data[which(data$X == "INCL10") + 1, ]

Upvotes: 1

Related Questions