Get line number for data frame R

The question is about data frames in R. I want to extract a row from a data frame along with its position (line number) in the original data frame. The idea is to create a new data frame that includes the row information extracted from the previous data frame, as well as the position of the row from the previous data frame.

patchLocalNo <- patchList[which(patchListTop5$sensitivity == patchLocalSpec),] 

what I want to do is take the line numbers from patchList that satisfy the condition and add them to the column in patchLocalNo.

Thanks in advance, I searched the Internet and asked employees, and all I can think of is data preprocessing in perl.

+3
source share
2 answers

I think you answered your question.

Results that () are a vector with line numbers you want to extract.

 df <- data.frame(x = runif(20)) w <- which(df$x > 0.9) w [1] 9 11 14 16 20 data.frame(which=w, df=df[w, ]) which df 1 9 0.9437138 2 11 0.9995509 3 14 0.9237888 4 16 0.9526003 5 20 0.9191727 
+8
source

You can do this in one or two lines of code:

 rNo <- as.integer(rownames(patchList[which(patchListTop5$sensitivity == patchLocalSpec),])) patchLocalNo <- cbind(patchLocalNo[rNames,], rNo) 
+1
source

Source: https://habr.com/ru/post/1398103/


All Articles