0

I have a datasets which contains same value.Please find the below datasets,

Parent
Child
Child
Child
Parent
Child
Child
Parent
Child
Child
Child
Child
Child
Child
Child
Child
Child
Child

I have to count the member of each parent. That means, on the 1st parent's member is 4,2nd parent's members are 3 etc.

I have already done this on excel by following formula,

=IFERROR(IF(B6="Parent",INDEX(MATCH("Parent",B7:B$23,0),1),""),COUNTA(B7:B$23)+1)`

Now, I want to achieve this things on R.

I have written following code

abc <- ifelse(ifelse(balor$Parent=="Parent", match("Parent",balor$Parent),""), count(balor,"Parent"))

but, got nothing in result.

Can you give me some hints of this problem?

Any suggestion is really appreciable.

R Code :

library(readr)
setwd("E:\\Buisness Analytics\\R\\PRACTISE")

balor <- read.csv("PersonalDeal-20180403083521.csv")

balor <- balor[,-1]
colnames(balor)[colnames(balor)=="Parent.Child"] <- "Parent"

library(plyr)

abc <-ifelse(ifelse(balor$Parent=="Parent",match("Parent",balor$Parent),""),count(balor,"Parent"))

head(abc)
# [1] NA NA NA NA NA NA
Frank
  • 66,179
  • 8
  • 96
  • 180
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap May 07 '18 at 15:23
  • @jeep, I know how to give reproducible example, but in this case, giving reprex code will not much differ than my previous posted question.Any way, I am going to reprex my R code. Hope , it will help to understand. – Sayan analytics May 07 '18 at 15:26
  • 1
    It's not clear if `balor$Parent` has "Parent" as a first element or if that's just the header name. Giving code to reproduce the table (per Jaap's links) could clarify. I suspect that `rle(cumsum(balor$Parent == "Parent"))` might be a step towards it, though... eg `rep(rep(c("Parent", "Child"), 3), c(1,3,1,2,1,10))` maybe. – Frank May 07 '18 at 15:26
  • 1
    Something like `ave(balor$Parent, cumsum(balor$Parent=="Parent"), FUN = length)`? – Jaap May 07 '18 at 15:26
  • @Jaap Or `tapply`. – Rui Barradas May 07 '18 at 15:33
  • @RuiBarradas there are several ways to skin a cat ;-) – Jaap May 07 '18 at 15:34
  • @Frank, I have given the code with reprex..Please go through of this.Hope it will help to understand.. – Sayan analytics May 07 '18 at 15:40
  • Could you describe what the desired outcome should be? (btw, the update is not a reproducible example: we don't have access to the csv-file) – Jaap May 07 '18 at 15:47
  • @Jaap, Sorry Jaap, I forgot to mention my desired outcome.. My expected output will be count of member of each parents.that means, on the 1st parents, number of members will be 4, 2nd parents, number of members should be 3 etc.. – Sayan analytics May 07 '18 at 15:51
  • In that case: Did you try the code I or @Frank provided? Did it give the intended result? if not, why? – Jaap May 07 '18 at 15:57
  • @Jaap, yes, your code has worked.I was getting some errors, but i have fixed it. Overall, your code and logic has worked. Thank you very much for your help. It was a new learning experience.Thanks a lot. – Sayan analytics May 07 '18 at 16:37

0 Answers0