2016-09-01 7 views
1

私は非常に特殊な問題があります。私は2つの条件に基づいてデータを抽出し、これの平均値を得るcsvテーブルを持っています。このコードのコードは、mean()とsubset()を組み合わせたときの問題

GDP <- mean(subset(World,World$Year==2013)$GDP_in_USD,na.rm=TRUE) 

World is my csv tableです。リストには、1960年から2015年の間に世界中のすべての国の異なる列のデータがあります。 2013年からGDP_in_USD列のすべての値を取得したい(国ごとに1つのセルが基本的に1つ)。

この関数を使用すると、値が数値でもbooleshでもないというエラーが表示されます。奇妙なことは、私の友人が私にコードを渡し、彼のコンピュータで働いたことです。それを再現しようとすると、エラーが発生します。 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") 

enter image description here

+0

'dput(head(World)) 'の出力を投稿して、データのサンプルを提供してください。 [投稿ガイドライン](http://stackoverflow.com/tags/r/info)を参照してください。 –

+0

"..."は文字として解釈されている可能性があります。 str(World)を実行して、列が期待するクラスであることを確認します。 – Dave2e

+0

ええ、あなたは単にそれを修正するかどうかを参照してください(as.numeric(World $ GDP_in_USD [World $ Year == 2013])、na.rm = T)を試してみてください。 – gfgm

答えて

0

あなたのデータ列名が区切られ、混乱です ""データは ";"で区切られています。上記のデータ構造が "df"と呼ばれる場合、ここでは可能な解決策です。

# your data from above 
# World<-structure(list(Country.Year. ...... 

#get names and split 
names<-strsplit(names(World), ".", fixed=TRUE)[[1]] 
#37 names are created but only 35 columns of data exist 
#removing the 2 most like errors 
names[15]<-paste0(names[15], names[16]) 
names[24]<-paste0(names[24], names[25]) 
names<-names[-c(16,25)] 

#now split the main body of the table 
temp<-sapply(World, function(x){strsplit(x, ";", fixed=TRUE)}) 
newdf<-as.data.frame(matrix(unlist(temp), ncol=35, byrow = TRUE)) 
#rename the columns 
names(newdf)<-names 
#convert the strings to numbers 
newdf[,2:35]<-apply(newdf[,2:35], 2, function(x){as.numeric(as.character(x))}) 

最も洗練されたコードではありませんが、正しい方向に向ける必要があります。

関連する問題