-1

Just as an illustrative example, to create a function similar to countif in excel, here is what I have tried to somehow use the string "mycolumn" inside the ddply "countif" variable definition below:

df <- c("a","a","b","c","c") %>% data.frame(stringsAsFactors = F)
colnames(df) <- "mycolumn"
x <- "mycolumn"
countif <- function(df,x ) {
y <- which(colnames(df)==x)
result1 <- ddply(df,x,nrow) #this works, but I can't use the x argument
result2 <- ddply(df,x,summarise, countif=length(df[,y])) #not working
result3 <- ddply(df,x,summarise, countif=length(parse(text=x))) #not working
    }

As you can see below, only result1 works, but I need a way to be able to use my mycolumn string in the ddply function instead of solely relying on nrow. Many thanks.

> result1
  mycolumn V1
1        a  2
2        b  1
3        c  2
> result2
  mycolumn countif
1        a       5
2        b       5
3        c       5
> result3
  mycolumn countif
1        a       1
2        b       1
3        c       1
gaut
  • 5,771
  • 1
  • 14
  • 45
  • Have you looked into do()? – SCDCE May 22 '18 at 12:50
  • Can you please elaborate? how would I use it in this case? – gaut May 22 '18 at 12:54
  • 1
    What is the 'if' in the that you're looking for? it seems that you just count them by groups (my column). You can get the same results with group_by() and summarize(). – DJV May 22 '18 at 12:55
  • Yes, I imagine there are other ways, but for other functions I need to understand how to convert "mycolumn" into mycolumn that ddply can understand. – gaut May 22 '18 at 12:59
  • 1
    @gpier `ddply` is kind of deprecated, you will find much easier to use its successors: `dplyr`, `tidyr` (generally called `tidyverse`, [have alook here](https://www.tidyverse.org/) – GGamba May 22 '18 at 13:11
  • @GGamba, thank you for the tip, what function in tidyverse would be similar to ddply? – gaut May 22 '18 at 13:17
  • see @Shinobi_atobe answer – GGamba May 22 '18 at 13:19

1 Answers1

1

not entirely sure if I get what you're after, but my best guess would be something like the below

library(dplyr)

df <-  data.frame(mycolumn = c("a","a","b","c","c"))

result1 <- df %>% group_by(mycolumn) %>% tally()

result3 <- df %>% filter(mycolumn %in% c("a", "b")) %>% group_by(mycolumn) %>% tally()

You can play around with the conditional inside the filter function

Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35
  • Yes, I imagine there are other ways, but for other functions I need to understand how to convert "mycolumn" into mycolumn that ddply can understand. Hoy can I use a string into ddply in general... – gaut May 22 '18 at 12:59
  • To have the same result as ddply, how would you then recombine result1 into df? – gaut May 23 '18 at 09:51