三天';s";巢;R中的函数不';t识别变量并打印:“;警告消息:未知或未初始化列“;
我正在使用一个数据集,该数据集有一列名为“ccode”的国家代码: 当我创建另一个列以创建名为“country”的国家名称时,我使用了从CRAN表下载的countrycode包中的函数“countrycode”,结果如下:三天';s";巢;R中的函数不';t识别变量并打印:“;警告消息:未知或未初始化列“;,r,nested,initialization,tidyr,tibble,R,Nested,Initialization,Tidyr,Tibble,我正在使用一个数据集,该数据集有一列名为“ccode”的国家代码: 当我创建另一个列以创建名为“country”的国家名称时,我使用了从CRAN表下载的countrycode包中的函数“countrycode”,结果如下: votes_processed <- votes %>% filter(vote <= 3) %>% mutate(year = session + 1945, country = countrycode(ccode,"co
votes_processed <- votes %>%
filter(vote <= 3) %>%
mutate(year = session + 1945,
country = countrycode(ccode,"cown","country.name"))
由于无法为这些国家/地区代码分配国家/地区名称,因此我将它们从数据框中筛选出来:
> table(is.na(votes_processed$country))
FALSE TRUE
350844 2703
> votes_processed <- filter(votes_processed,!is.na(country))
> table(is.na(votes_processed$country))
FALSE
350844
有人能给我解释一下这个“国家”专栏到底发生了什么,为什么R不认识它,我能做些什么
我是这个平台的初学者。我收到一条要求提供数据样本的评论,我将其粘贴在这里:
rcid<-c(5168,4317,3598,2314,1220,5024,3151,2042,2513,238,4171,3748,2595,
5160,4476,308,3621,874,2025,3793,3595,1191,987,1207,2255,211,
2585,2319,3590,189)
session<- c(66,56,46,36,26,64,42,34,38,4,54,48,38,66,58,6,46,18,34,
48,46,26,22,26,36,4,38,36,46,4)
vote<- c(1,8,1,8,9,1,3,2,2,9,2,1,3,1,1,1,1,1,1,1,1,1,9,2,1,9,1,1,1,2)
ccode<-as.integer(c(816,816,816,816,816,816,260,260,260,260,2,42,2,20,
31,41,20,42,41,31,70,95,80,93,58,51,53,90,55,90))
sample_data_votes<-data.frame("rcid"=rcid,"session"=session, "vote"= vote,
"ccode"=ccode)
rcid按年份\u国家/地区进行分组,因此您需要先取消分组,然后进行嵌套
library(tidyverse)
by_year_country %>% ungroup() %>%
nest(-country) %>% head(n=2)
# A tibble: 2 x 2
country data
<chr> <list>
1 Guatemala <tibble [2 x 3]>
2 Haiti <tibble [2 x 3]>
库(tidyverse)
按年份\u国家%>%ungroup()%%>%
巢(-country)%>%头(n=2)
#一个tibble:2x2
国家数据
1危地马拉
2海地
按年份\u国家/地区进行分组,因此您需要先取消分组,然后进行嵌套
library(tidyverse)
by_year_country %>% ungroup() %>%
nest(-country) %>% head(n=2)
# A tibble: 2 x 2
country data
<chr> <list>
1 Guatemala <tibble [2 x 3]>
2 Haiti <tibble [2 x 3]>
库(tidyverse)
按年份\u国家%>%ungroup()%%>%
巢(-country)%>%头(n=2)
#一个tibble:2x2
国家数据
1危地马拉
2海地
看起来您需要从调用nest
时删除-country
部分
library(dplyr)
library(tidyr)
library(countrycode)
rcid<-c(5168,4317,3598,2314,1220,5024,3151,2042,2513,238,4171,3748,2595,
5160,4476,308,3621,874,2025,3793,3595,1191,987,1207,2255,211,
2585,2319,3590,189)
session<- c(66,56,46,36,26,64,42,34,38,4,54,48,38,66,58,6,46,18,34,
48,46,26,22,26,36,4,38,36,46,4)
vote<- c(1,8,1,8,9,1,3,2,2,9,2,1,3,1,1,1,1,1,1,1,1,1,9,2,1,9,1,1,1,2)
ccode<-as.integer(c(816,816,816,816,816,816,260,260,260,260,2,42,2,20,
31,41,20,42,41,31,70,95,80,93,58,51,53,90,55,90))
votes<-data.frame("rcid"=rcid,"session"=session, "vote"= vote,
"ccode"=ccode)
votes_processed <- votes %>%
filter(vote <= 3) %>%
mutate(year = session + 1945,
country = countrycode(ccode,"cown","country.name")) %>%
filter(!is.na(country))
by_year_country <- votes_processed %>%
group_by(year,country) %>%
summarize(total = n(),
percent_yes = mean(vote == 1))
nested <- by_year_country %>%
nest()
库(dplyr)
图书馆(tidyr)
图书馆(国家代码)
rcid看起来您需要从调用nest
library(dplyr)
library(tidyr)
library(countrycode)
rcid<-c(5168,4317,3598,2314,1220,5024,3151,2042,2513,238,4171,3748,2595,
5160,4476,308,3621,874,2025,3793,3595,1191,987,1207,2255,211,
2585,2319,3590,189)
session<- c(66,56,46,36,26,64,42,34,38,4,54,48,38,66,58,6,46,18,34,
48,46,26,22,26,36,4,38,36,46,4)
vote<- c(1,8,1,8,9,1,3,2,2,9,2,1,3,1,1,1,1,1,1,1,1,1,9,2,1,9,1,1,1,2)
ccode<-as.integer(c(816,816,816,816,816,816,260,260,260,260,2,42,2,20,
31,41,20,42,41,31,70,95,80,93,58,51,53,90,55,90))
votes<-data.frame("rcid"=rcid,"session"=session, "vote"= vote,
"ccode"=ccode)
votes_processed <- votes %>%
filter(vote <= 3) %>%
mutate(year = session + 1945,
country = countrycode(ccode,"cown","country.name")) %>%
filter(!is.na(country))
by_year_country <- votes_processed %>%
group_by(year,country) %>%
summarize(total = n(),
percent_yes = mean(vote == 1))
nested <- by_year_country %>%
nest()
库(dplyr)
图书馆(tidyr)
图书馆(国家代码)
RCIDW在寻求帮助时,您应该提供一个简单的示例输入和所需输出,用于测试和验证可能的解决方案。数据的图片不便于实际运行和测试代码。还要确保指定此countrycode
函数的来源。@MrFlick非常感谢您的评论。我编辑这个问题是为了添加示例数据,并解释说我从CRAN获得了countrycode包。在寻求帮助时,您应该包括一个简单的示例输入和所需的输出,用于测试和验证可能的解决方案。数据的图片不便于实际运行和测试代码。还要确保指定此countrycode
函数的来源。@MrFlick非常感谢您的评论。我编辑了这个问题来添加示例数据,并解释说我从CRAN获得了countrycode包。非常感谢!我想按国家分类筑巢,所以先把它们分开帮助我做到了。非常感谢!我想按国家分类筑巢,因此,先把它们分门别类有助于我做到这一点。
library(tidyverse)
by_year_country %>% ungroup() %>%
nest(-country) %>% head(n=2)
# A tibble: 2 x 2
country data
<chr> <list>
1 Guatemala <tibble [2 x 3]>
2 Haiti <tibble [2 x 3]>
library(dplyr)
library(tidyr)
library(countrycode)
rcid<-c(5168,4317,3598,2314,1220,5024,3151,2042,2513,238,4171,3748,2595,
5160,4476,308,3621,874,2025,3793,3595,1191,987,1207,2255,211,
2585,2319,3590,189)
session<- c(66,56,46,36,26,64,42,34,38,4,54,48,38,66,58,6,46,18,34,
48,46,26,22,26,36,4,38,36,46,4)
vote<- c(1,8,1,8,9,1,3,2,2,9,2,1,3,1,1,1,1,1,1,1,1,1,9,2,1,9,1,1,1,2)
ccode<-as.integer(c(816,816,816,816,816,816,260,260,260,260,2,42,2,20,
31,41,20,42,41,31,70,95,80,93,58,51,53,90,55,90))
votes<-data.frame("rcid"=rcid,"session"=session, "vote"= vote,
"ccode"=ccode)
votes_processed <- votes %>%
filter(vote <= 3) %>%
mutate(year = session + 1945,
country = countrycode(ccode,"cown","country.name")) %>%
filter(!is.na(country))
by_year_country <- votes_processed %>%
group_by(year,country) %>%
summarize(total = n(),
percent_yes = mean(vote == 1))
nested <- by_year_country %>%
nest()