0

I have a data below

test=data.frame("name"=c("A","A","A","A","B","B","C"), value=c(10,11,12,13,14,15,16))

I want to subset the test data based on non-repeated name "C", I want to show the data below:

name value
   c    16

I try test[table(test$name)>1,], but the output was wrong.

Please give me some hint, thanks!!

Prradep
  • 5,506
  • 5
  • 43
  • 84
swchen
  • 643
  • 2
  • 8
  • 24
  • following on your logic you could do: `test[test$name==names(which(table(test$name)==1)),]` or you could use `library(dplyr)` and do `test%>% group_by(name)%>% filter(length(name)==1)` – Haboryme Nov 10 '16 at 08:23

4 Answers4

4

We can use data.table. Convert the 'data.frame' to 'data.table' (setDT(test)), grouped by 'name', if the number of rows is equal to 1 (.N==1), subset the data.table.

library(data.table)
setDT(test)[, if(.N==1) .SD, by = name]
#   name value
#1:    C    16
akrun
  • 874,273
  • 37
  • 540
  • 662
2

We can try ave function in base R and select only the group with length = 1

test[ave(test$value, test$name, FUN = function(x) length(x)) == 1, ]

# name value
#7    C    16

Or with dplyr

library(dplyr)
test %>%
   group_by(name) %>%
   filter(n() == 1)

#   name value
#   <fctr> <dbl>
#1     C    16
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

using data.frame as such,

table.freq <- as.data.frame(table(test$name))
test[test$name %in% table.freq$Var1[table.freq$Freq==1],]
#  name value
#7    C    16

or using which

test[test$name %in% names(which(table(test$name)==1)),]
#  name value
#7    C    16
Prradep
  • 5,506
  • 5
  • 43
  • 84
2

This is an alternate code for data.frame:

test <-
  data.frame(
   name = c("A", "A", "A", "A", "B", "B", "C", "D"),
   value = c(10, 11, 12, 13, 14, 15, 16, 17)
)

test[test$name == unique(test$name)[table(test$name) == 1], ]
# Answer:
# name value
# 7    C    16
# 8    D    17

Or:

test[test$name == names(table(test$name))[table(test$name) == 1], ]
Enrique Pérez Herrero
  • 3,699
  • 2
  • 32
  • 33
  • 1
    I feel using `%in%` would be a better approach instead of `==` as the former will pick more than one unique variable present. But this can also be a solution for this as there is only one unique variable in this object. – Prradep Nov 10 '16 at 09:22