0

I have the following problem. Suppose I have a column in the data frame, the values in one column are vectors and for each row of data I would like to count the number of elements in that particular vector, eg.

ID, brands
1, c("brand1", "brand2", "brand3")
2, c("brand4")
3, c("brand5", "brand7")

and using dplyr I would like to add 'counter' column which tells me how many elements has particular vector in the row, eg.

ID, brands, counter
1, c("brand1", "brand2", "brand3"), 3
2, c("brand4"), 1
3, c("brand5", "brand7"), 2

I used counter = length(brands) but it gives the total number of rows in the frame.

Heikki
  • 2,214
  • 19
  • 34
krakowi
  • 583
  • 5
  • 17
  • 4
    Please use `dput()` to provide a reproducible example of your data as described [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – tobiasegli_te Nov 20 '17 at 11:11
  • `dplyr::n_distinct(brands)` ? – zacdav Nov 20 '17 at 11:13
  • for daTA - `df <- data.table(ID = c(1,2,3), brands = c(c('brand1, brand2, brand3'), c('brand4'), c('brand5, brand7')))` – Hardik Gupta Nov 20 '17 at 11:17
  • `n_distinct(brands)` returns the total number of rows, the same result I have with `lenght(brands)` – krakowi Nov 20 '17 at 11:20

1 Answers1

3

Use lengths instead of length:

df <- data.frame(
  ID=1:3, 
  brands=I(list(
    c("brand1", "brand2", "brand3"),
    c("brand4"),
    c("brand5", "brand7")
  ))
)
df$n <- lengths(df$brands)           
df
# ID       brands n
# 1  1 brand1, .... 3
# 2  2       brand4 1
# 3  3 brand5, .... 2
lukeA
  • 53,097
  • 5
  • 97
  • 100