【发布时间】:2016-09-01 19:29:40
【问题描述】:
我有一个非常具体的问题。我有一个 csv 表,我想根据两个条件从中提取数据并获取它的 mean()。我的代码是:
GDP <- mean(subset(World,World$Year==2013)$GDP_in_USD,na.rm=TRUE)
世界是我的 csv 表。在列表中,我有来自 1960-2015 年全球所有国家/地区的不同列的数据。我想获得 2013 年 GDP_in_USD 列的所有值(基本上每个国家一个单元格)。
当我使用这个函数时,我得到的错误是值既不是数字也不是布尔值。奇怪的是,我的一个朋友给了我代码,它在他的电脑上运行。当我尝试重现它时,我得到了错误。要读取 csv 表,我使用:
World <- read.csv("World2.csv", header=TRUE, sep=delim, dec=dec, stringsAsFactors=FALSE)
什么可能导致问题?如果您需要更多信息,请告诉我。
structure(list(Country.Year.Zeitraum_NR.Agriculture_value_added_percent_of_GDP.Central_government_debt_total_percent_of_GDP.Cost_to_export_USD_per_container.Cost_to_import_USD_per_container.Employment_in_agriculture_percent_of_total_employment.Employment_in_industry_percent_of_total_employment.Employment_in_services_percent_of_total_employment.Exports_of_goods_and_services_percent_of_GDP.Final_consumption_expenditure_etc_percent_of_GDP.Foreign_direct_investment_net_inflows_percent_of_GDP.Foreign_direct_investment_net_outflows_percent_of_GDP.General_government_final_consumption_expenditure_._of_GDP.GDP_growth_annual_percent.Government_expenditure_on_education_total_percent_of_GDP.Household_final_consumption_expenditure_etc_percent_of_GDP.Imports_of_goods_and_services_percent_of_GDP.Industry_value_added_percent_of_GDP.Inflation_consumer_prices_annual_percent.Lending_interest_rate_percent.Patent_applications_residents_._nonresidents.Research_and_development_expenditure_percent_of_GDP.Services_etc_value_added_percent_of_GDP.Subsidies_and_other_transfers_percent_of_expense.Tariff_rate_applied_simple_mean_all_products_percent.Taxes_on_exports_percent_of_tax_revenue.Taxes_on_goods_and_services_percent_of_revenue.Taxes_on_income_profits_and_capital_gains_percent_of_revenue.Taxes_on_international_trade_percent_of_revenue.Total_tax_rate_percent_of_commercial_profits.Trade_percent_of_GDP.Unemployment_total_percent_of_total_labor_force_national_estimate.GDP_in_USD = c("Afghanistan;1960;1;..;..;..;..;..;..;..;4.132233258;86.77685029;..;..;..;..;..;..;7.024793471;..;..;..;..;..;..;..;..;..;..;..;..;..;11.15702673;..;537777811.91",
"Afghanistan;1961;1;..;..;..;..;..;..;..;4.453443322;87.0445247;..;..;..;..;..;..;8.097166426;..;..;..;..;..;..;..;..;..;..;..;..;..;12.55060975;..;548888894.58",
"Afghanistan;1962;1;..;..;..;..;..;..;..;4.878051281;85.36583991;..;..;..;..;..;..;9.349593301;..;..;..;..;..;..;..;..;..;..;..;..;..;14.22764458;..;546666678.04",
"Afghanistan;1963;1;..;..;..;..;..;..;..;9.171601205;93.49111965;..;..;..;..;..;..;16.86391035;..;..;..;..;..;..;..;..;..;..;..;..;..;26.03551156;..;751111190.76",
"Afghanistan;1964;1;..;..;..;..;..;..;..;8.88889265;95.2777688;..;..;..;..;..;..;18.05555524;..;..;..;..;..;..;..;..;..;..;..;..;..;26.94444789;..;800000045.51",
"Afghanistan;1965;1;..;..;..;..;..;..;..;11.25827903;98.89624551;..;..;..;..;..;..;21.41280357;..;..;..;..;..;..;..;..;..;..;..;..;..;32.6710826;..;1006666638.22"
)), .Names = "Country.Year.Zeitraum_NR.Agriculture_value_added_percent_of_GDP.Central_government_debt_total_percent_of_GDP.Cost_to_export_USD_per_container.Cost_to_import_USD_per_container.Employment_in_agriculture_percent_of_total_employment.Employment_in_industry_percent_of_total_employment.Employment_in_services_percent_of_total_employment.Exports_of_goods_and_services_percent_of_GDP.Final_consumption_expenditure_etc_percent_of_GDP.Foreign_direct_investment_net_inflows_percent_of_GDP.Foreign_direct_investment_net_outflows_percent_of_GDP.General_government_final_consumption_expenditure_._of_GDP.GDP_growth_annual_percent.Government_expenditure_on_education_total_percent_of_GDP.Household_final_consumption_expenditure_etc_percent_of_GDP.Imports_of_goods_and_services_percent_of_GDP.Industry_value_added_percent_of_GDP.Inflation_consumer_prices_annual_percent.Lending_interest_rate_percent.Patent_applications_residents_._nonresidents.Research_and_development_expenditure_percent_of_GDP.Services_etc_value_added_percent_of_GDP.Subsidies_and_other_transfers_percent_of_expense.Tariff_rate_applied_simple_mean_all_products_percent.Taxes_on_exports_percent_of_tax_revenue.Taxes_on_goods_and_services_percent_of_revenue.Taxes_on_income_profits_and_capital_gains_percent_of_revenue.Taxes_on_international_trade_percent_of_revenue.Total_tax_rate_percent_of_commercial_profits.Trade_percent_of_GDP.Unemployment_total_percent_of_total_labor_force_national_estimate.GDP_in_USD", row.names = c(NA,
6L), class = "data.frame")
【问题讨论】:
-
请通过发布
dput(head(World))的输出来提供您的数据样本。请参阅posting guidelines。 -
很可能“...”被解释为一个字符。运行 str(World) 以验证列是您期望的类。
-
是的,你可以试试 mean(as.numeric(World$GDP_in_USD[World$Year==2013]), na.rm=T),看看能不能解决问题。
-
我使用了 na.strings 行来避免误解 NA 的问题。 GabrielFGM:结果是 NAN,让我更加困惑。 @WeihuangWong:我会把它添加到主评论中,但是由于我的变量名很长,它读起来不太好看。
-
@Dave2e:我有一个问题,我没有像您期望的那样得到一个漂亮的表(并且我会从另一个数据表中得到)。更像是我的数据是一个大博客,基本没用。什么可能导致问题?这个平台上没有办法为我作为新用户发送私人消息,对吗?否则我会把整张桌子发给你,这样你就可以看得更清楚了