问题
I have a data frame called df like this:
Author_ID Country Cited Name Title
1: 1 Spain 10 Alex Whatever
2: 1 France 15 Ale Whatever2
3: 1 NA 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 NA 10 Alce Whatever8
8: 2 NA 10 Alce Whatever8
And I would like to get something like this, where the NA are replaced for the Country that most times appears for that Author_ID (if there are two countries that appear the same number of times, random between those two would be good):
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
8: 2 Greece 10 Alce Whatever8
Thanks in advance.
回答1:
With 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
In base 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
with 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
If several country appear a maximum of time it will take the first one, not random.
If countries are ONLY NA for some authors
first call this to modify example data:
df$Country[df$Author_ID ==2] <- NA
Then here are the 3 adapted solutions, not super elegant but it works. I suspect there might be a base/dplyr/data.table function to change zero length elements to NA
more smoothly.
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)
来源:https://stackoverflow.com/questions/50659096/convert-na-to-most-appearing-value-based-in-another-column