1

I have a dataframe with unique values $Number identifying specific points where a polygon is intersecting. Some points (i.e. 56) have 3 polygons that intersect. I want to extract the three rows which start with 56.

    df <- cbind(Number = rownames(check), check)
    df

df table

The issue going forward is I will be applying this for 10,000 points and won't know the repeating number such as "56". So is there a way to have a general expression which chooses rows with a general match without knowing that value?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Harmzy15
  • 487
  • 3
  • 6
  • 15
  • 1
    Please don't include pictures of data. Create a proper [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output for that data. – MrFlick Feb 24 '16 at 22:03

3 Answers3

1

You can achieve the desired output with:

subset2 <- function(n) df[floor(df$Number) == n,]

where df is the name of your dataset and Number is the name of the target column. We can fill in n as needed:

#Example
df <- data.frame(Number=c(1,3,24,56.65,56.99,56.14,66),y=sample(LETTERS,7))
df
#   Number y
# 1   1.00 J
# 2   3.00 B
# 3  24.00 D
# 4  56.65 R
# 5  56.99 I
# 6  56.14 H
# 7  66.00 V

subset2(56)
#   Number y
# 4  56.65 R
# 5  56.99 I
# 6  56.14 H
Pierre L
  • 28,203
  • 6
  • 47
  • 69
1

I simply changed the $Number column into a numeric field, then rounded down to integer data.

    numeric <- as.numeric(as.character(df$Number))
    Id <- floor(numeric)
Harmzy15
  • 487
  • 3
  • 6
  • 15
0

If we only want $Number with more than 3 counts then we can use dplyr to group by $Number and then retain $Number if it has more than 3 counts

library(dplyr)

# Data
df <- data.frame(Number = c(1,1,1,2,2,3,3))

# Filtering
df %>% group_by(Number) %>% filter(n() >= 3)
shackett
  • 323
  • 2
  • 5