1

I have this example: df.Journal.Conferences

venue author0 author1 author2 ... author19
A     John    Mary
B     Peter   Jacob   Isabella  
C     Lia
B     Jacob   Lara    John
C     Mary
B     Isabella

I want to know how many unique authors are in each venue

Result:

A 2
B 5
C 2

Edit: Here is the link to my data: GoogleDrive Excel sheet.

zx8754
  • 52,746
  • 12
  • 114
  • 209
ABueno
  • 35
  • 3

3 Answers3

0

because your data was hard to reproduce, I generated a "similar" data set, this should word

set.seed(1984)
df <- data.frame(id = sample(1:5,10, replace= T), 
                 v1 = sample(letters[1:5],10,replace= T),
                 v2 = sample(letters[1:5],10,replace= T),
                 v3 = sample(letters[1:5],10,replace= T),
                 v4 = sample(letters[1:5],10,replace= T), 
                 stringsAsFactors = F)


z <- data.frame( id = unique(df$id), n = NA )

for (i in z$id)  {

  z$n[z$id == i] <- length(unique(unlist(df[df$id == i,-1])))

}

z
#   id n
# 1  4 4
# 2  3 4
# 3  2 4
# 4  5 4
# 5  1 3
Mouad_Seridi
  • 2,666
  • 15
  • 27
0

Using dplyr and tidyr, reshape the data from wide to long, then group by count.

library(dplyr)
library(tidyr)

gather(df1, key = author, value = name, -venue) %>% 
  select(venue, name) %>% 
  group_by(venue) %>% 
  summarise(n = n_distinct(name, na.rm = TRUE))
# # A tibble: 3 × 2
#   venue     n
#   <chr> <int>
# 1     A     2
# 2     B     5
# 3     C     2

data

df1 <- read.table(text ="
venue,author0,author1,author2
A,John,Mary,NA
B,Peter,Jacob,Isabella
C,Lia,NA,NA
B,Jacob,Lara,John
C,Mary,NA,NA
B,Isabella,NA,NA
", header = TRUE, sep = ",", stringsAsFactors = FALSE)

Edit: Saved your Excel sheet as CSV, then read in using read.csv, then above code returns below output:

df1 <- read.csv("Journal_Conferences_Authors.csv", na.strings = "#N/A")

# output

# # A tibble: 427 × 2
#                                     venue     n
#                                    <fctr> <int>
# 1                                    AAAI     4
# 2                                     ACC     4
# 3                               ACIS-ICIS     5
# 4  ACM SIGSOFT Software Engineering Notes     1
# 5       ACM Southeast Regional Conference     5
# 6                                ACM TIST     3
# 7       ACM Trans. Comput.-Hum. Interact.     3
# 8                                    ACML     2
# 9                                    ADMA     2
# 10             Advanced Visual Interfaces     3
# # ... with 417 more rows
zx8754
  • 52,746
  • 12
  • 114
  • 209
0

Using @zx8754 data for testing, this code gives want you wanted (assuming you have NA for empty cells in the dataframe):

sapply(split(df1[,-1], df1$venue), function(x) length(unique(x[!is.na(x)])))
# A B C 
# 2 5 2 
Osdorp
  • 190
  • 7
  • It must count unique values. The result must be `# A 2 # B 5 # C 2` – ABueno May 12 '17 at 13:29
  • Sorry, then should be: `sapply(split(df1[,-1], df1$venue), function(x) length(unique(x[!is.na(x)])))`. I will edit it. – Osdorp May 12 '17 at 13:33