С data.table
library(data.table)
# setDT(df)
df[,Country := replace(Country,is.na(Country),names(which.max(table(Country)))),by=Author_ID]
# Author_ID Country Cited Name Title
# 1: 1 Spain 10 Alex Whatever
# 2: 1 France 15 Ale Whatever2
# 3: 1 Spain 10 Alex Whatever3
# 4: 1 Spain 10 Alex Whatever4
# 5: 2 Italy 10 Alice Whatever5
# 6: 2 Greece 10 Alice Whatever6
# 7: 2 Greece 10 Alice Whatever7
# 8: 2 Greece 10 Alce Whatever8
# 9: 2 Greece 10 Alce Whatever8
В базе R
:
df$Country <- unlist(tapply(df$Country,df$Author_ID,function(x)
replace(x,is.na(x),names(which.max(table(x))))))
# Author_ID Country Cited Name Title
# 1 1 Spain 10 Alex Whatever
# 2 1 France 15 Ale Whatever2
# 3 1 Spain 10 Alex Whatever3
# 4 1 Spain 10 Alex Whatever4
# 5 2 Italy 10 Alice Whatever5
# 6 2 Greece 10 Alice Whatever6
# 7 2 Greece 10 Alice Whatever7
# 8 2 Greece 10 Alce Whatever8
# 9 2 Greece 10 Alce Whatever8
с dplyr
:
library(dplyr)
df %>% group_by(Author_ID) %>%
mutate(Country = replace(
Country,
is.na(Country),
names(which.max(table(Country)))))
# # A tibble: 9 x 5
# # Groups: Author_ID [2]
# Author_ID Country Cited Name Title
# <int> <chr> <int> <chr> <chr>
# 1 1 Spain 10 Alex Whatever
# 2 1 France 15 Ale Whatever2
# 3 1 Spain 10 Alex Whatever3
# 4 1 Spain 10 Alex Whatever4
# 5 2 Italy 10 Alice Whatever5
# 6 2 Greece 10 Alice Whatever6
# 7 2 Greece 10 Alice Whatever7
# 8 2 Greece 10 Alce Whatever8
# 9 2 Greece 10 Alce Whatever8
Если появляется несколько странМаксимальное время, которое потребуется первому, а не случайному.
Если страны являются ТОЛЬКО NA для некоторых авторов
, сначала вызовите это, чтобы изменить пример данных:
df$Country[df$Author_ID ==2] <- NA
Тогда вот 3 адаптированных решения, не супер элегантных, но это работает.Я подозреваю, что может быть функция base / dplyr / data.table для более плавного изменения элементов нулевой длины на NA
.
setDT(df)
df[,Country := replace(Country,is.na(Country),{
nm <- names(which.max(table(x)))
if(length(nm)==0) NA else nm}),
by=Author_ID]
df <- df[!is.na(df$Country),]
# Author_ID Country Cited Name Title
# 1: 1 Spain 10 Alex Whatever
# 2: 1 France 15 Ale Whatever2
# 3: 1 Spain 10 Alex Whatever4
df$Country <- unlist(tapply(df$Country,df$Author_ID,function(x)
replace(x,is.na(x),{
nm <- names(which.max(table(x)))
if(length(nm)==0) NA else nm
})))
df <- df[!is.na(df$Country),]
# Author_ID Country Cited Name Title
# 1 1 Spain 10 Alex Whatever
# 2 1 France 15 Ale Whatever2
# 3 1 Spain 10 Alex Whatever3
# 4 1 Spain 10 Alex Whatever4
df %>% group_by(Author_ID) %>%
mutate(Country = replace(
Country,
is.na(Country),
names(which.max(table(Country))) %>%
{if(length(.)==0) NA else .})) %>%
filter(!is.na(Country))
# # A tibble: 4 x 5
# # Groups: Author_ID [1]
# Author_ID Country Cited Name Title
# <int> <chr> <int> <chr> <chr>
# 1 1 Spain 10 Alex Whatever
# 2 1 France 15 Ale Whatever2
# 3 1 Spain 10 Alex Whatever3
# 4 1 Spain 10 Alex Whatever4
data
df <- read.table(text="Author_ID Country Cited Name Title
1 Spain 10 Alex Whatever
1 France 15 Ale Whatever2
1 NA 10 Alex Whatever3
1 Spain 10 Alex Whatever4
2 Italy 10 Alice Whatever5
2 Greece 10 Alice Whatever6
2 Greece 10 Alice Whatever7
2 NA 10 Alce Whatever8
2 NA 10 Alce Whatever8",h=T,strin=F)