Rescue indexes from my data elements.frame

Asked

Viewed 289 times

0

I have a data.frame that is filtered from another data.frame. (see image below)

inserir a descrição da imagem aqui

However, I would like to filter this data.frame again and retrieve its original indexes in a vector.

Example: Let’s say that Filtrei and the result were the items 1(Jardim), 11(apiário) e 28(museu)

I want a vector V[1,11,28], that is, with the original indexes.

All I tried to do was get the sequential indexes back. example [1,2,3,4,5,6...,11].

Other Example:

In my date.frame I have a column with the following vector -> 16.2 0.4 21.0 18.8 0.8 0.8 18.1 1.5 21.1 21.0 1.9

I made a filter to show the lines of my date.frama that were in the column mentioned above values less than or equal to 5.

after the filter the column mentioned above was as follows: 0.4 0.8 0.8 1.5 1.9

after the filter my data.frame was as shown in the figure below: inserir a descrição da imagem aqui

However, I want to recover the indexes of the corresponding lines the filter information in a vector, following the example, my final answer would be v = [3,12,13,16,40]

Any help or tip on how to do this??

1 answer

0

set.seed(42)

Suppose you have the following data.frame:

let = rep(letters[1:5], 3)
valor = rnorm(length(let))
dat = data.frame(let, valor)

#   let       valor
# 1    a  1.37095845
# 2    b -0.56469817
# 3    c  0.36312841
# 4    d  0.63286260
# 5    e  0.40426832
# 6    a -0.10612452
# 7    b  1.51152200
# 8    c -0.09465904
# 9    d  2.01842371
# 10   e -0.06271410
# 11   a  1.30486965
# 12   b  2.28664539
# 13   c -1.38886070
# 14   d -0.27878877
# 15   e -0.13332134

Now you filter only the letters a:

dat2 = dat[which(dat$let == "a"), ]

#     let      valor
# 1    a  1.3709584
# 6    a -0.1061245
# 11   a  1.3048697

To find the contents of a of the original data frame:

v = which(dat$let == dat2$let)

# [1]  1  6 11
  • In my case I only have this error message " longer Object length is not a Multiple of Shorter Object length"

  • original vector: 16.2 0.4 21.0 18.8 0.8 0.8 18.1 1.5 21.1 21.0 1.9

  • vetor filtro: 0.4 0.8 0.8 1.5 1.9

  • I did the same procedure, comparing the original with the filtered one to obtain the indices

  • My filter in the case is smaller or equal to 5 (in my code this value will be dynamic)

  • In case these vectors are columns of my date.

  • You can edit your question with this attempt?

  • I edited the question.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.