我有一个CSV文件,其中包含18岁时身高的数据,并且每年有所不同。 (data.csv)(您可以访问它here。)
我想做的就是将所有数据分成带有国家名称的不同文件。
countryList <- unique(data[,1])
for (countries in countryList) {
print(countries)
print(class(countries))
output <- data %>%
select(`Country`, `Sex`, `Year of birth`, `Mean height (cm)`) %>%
arrange(`Country`)
write.table(output, paste("~/Desktop/data/", countries, ".csv", sep=""), col.names=TRUE, sep=",")
}
输出的结果不是得到一堆带有国家名称的文件,而是输出:
[1] "Afghanistan" "Albania"
[3] "Algeria" "American Samoa"
[5] "Andorra" "Angola"
[7] "Antigua and Barbuda" "Argentina"
[9] "Armenia" "Australia"
[11] "Austria" "Azerbaijan"
[13] "Bahamas" "Bahrain"
[15] "Bangladesh" "Barbados"
[17] "Belarus" "Belgium"
[19] "Belize" "Benin"
[21] "Bermuda" "Bhutan"
[23] "Bolivia" "Bosnia and Herzegovina"
[25] "Botswana" "Brazil"
[27] "Brunei Darussalam" "Bulgaria"
[29] "Burkina Faso" "Burundi"
[31] "Cabo Verde" "Cambodia"
[33] "Cameroon" "Canada"
[35] "Central African Republic" "Chad"
[37] "Chile" "China"
[39] "China (Hong Kong SAR)" "Colombia"
[41] "Comoros" "Congo"
[43] "Cook Islands" "Costa Rica"
[45] "Cote d'Ivoire" "Croatia"
[47] "Cuba" "Cyprus"
[49] "Czech Republic" "Denmark"
[51] "Djibouti" "Dominica"
[53] "Dominican Republic" "DR Congo"
[55] "Ecuador" "Egypt"
[57] "El Salvador" "Equatorial Guinea"
[59] "Eritrea" "Estonia"
[61] "Ethiopia" "Fiji"
[63] "Finland" "France"
[65] "French Polynesia" "Gabon"
[67] "Gambia" "Georgia"
[69] "Germany" "Ghana"
[71] "Greece" "Greenland"
[73] "Grenada" "Guatemala"
[75] "Guinea" "Guinea Bissau"
[77] "Guyana" "Haiti"
[79] "Honduras" "Hungary"
[81] "Iceland" "India"
[83] "Indonesia" "Iran"
[85] "Iraq" "Ireland"
[87] "Israel" "Italy"
[89] "Jamaica" "Japan"
[91] "Jordan" "Kazakhstan"
[93] "Kenya" "Kiribati"
[95] "Kuwait" "Kyrgyzstan"
[97] "Lao PDR" "Latvia"
[99] "Lebanon" "Lesotho"
[101] "Liberia" "Libya"
[103] "Lithuania" "Luxembourg"
[105] "Macedonia (TFYR)" "Madagascar"
[107] "Malawi" "Malaysia"
[109] "Maldives" "Mali"
[111] "Malta" "Marshall Islands"
[113] "Mauritania" "Mauritius"
[115] "Mexico" "Micronesia (Federated States of)"
[117] "Moldova" "Mongolia"
[119] "Montenegro" "Morocco"
[121] "Mozambique" "Myanmar"
[123] "Namibia" "Nauru"
[125] "Nepal" "Netherlands"
[127] "New Zealand" "Nicaragua"
[129] "Niger" "Nigeria"
[131] "Niue" "North Korea"
[133] "Norway" "Occupied Palestinian Territory"
[135] "Oman" "Pakistan"
[137] "Palau" "Panama"
[139] "Papua New Guinea" "Paraguay"
[141] "Peru" "Philippines"
[143] "Poland" "Portugal"
[145] "Puerto Rico" "Qatar"
[147] "Romania" "Russian Federation"
[149] "Rwanda" "Saint Kitts and Nevis"
[151] "Saint Lucia" "Saint Vincent and the Grenadines"
[153] "Samoa" "Sao Tome and Principe"
[155] "Saudi Arabia" "Senegal"
[157] "Serbia" "Seychelles"
[159] "Sierra Leone" "Singapore"
[161] "Slovakia" "Slovenia"
[163] "Solomon Islands" "Somalia"
[165] "South Africa" "South Korea"
[167] "Spain" "Sri Lanka"
[169] "Sudan" "Suriname"
[171] "Swaziland" "Sweden"
[173] "Switzerland" "Syrian Arab Republic"
[175] "Taiwan" "Tajikistan"
[177] "Tanzania" "Thailand"
[179] "Timor-Leste" "Togo"
[181] "Tokelau" "Tonga"
[183] "Trinidad and Tobago" "Tunisia"
[185] "Turkey" "Turkmenistan"
[187] "Tuvalu" "Uganda"
[189] "Ukraine" "United Arab Emirates"
[191] "United Kingdom" "United States of America"
[193] "Uruguay" "Uzbekistan"
[195] "Vanuatu" "Venezuela"
[197] "Viet Nam" "Yemen"
[199] "Zambia" "Zimbabwe"
[1] "character"
此错误:
Error in file(file, ifelse(append, "a", "w")) :
invalid 'description' argument
In addition: Warning message:
In if (file == "") file <- stdout() else if (is.character(file)) { :
the condition has length > 1 and only the first element will be used
我仍然是R语言的初学者。任何帮助将不胜感激。
答案 0 :(得分:1)
这段代码对我来说很好。我重新排列了一下代码。确保要写入的目录存在,否则将失败。
library(tidyverse)
data.by.country <- data %>%
group_by(Country) %>%
select("Country","Sex","Year.of.birth","Mean.height..cm.") %>%
split(.$Country)
YourDir <- "ENTER YOUR DIR"
if(!dir.exists(YourDir)) dir.create(YourDir,recursive=T)
lapply(names(data.by.country),function(nam)
{
print(nam)
write.table(data.by.country[[nam]],paste0(YourDir, nam, ".csv"),col.names=T,sep=",")
})
答案 1 :(得分:0)
下面的代码将完成您的工作,而无需使用for循环或多余的代码
country <- read.csv("country_split.csv")
country_list <-split(country, as.factor(country$Country))
lapply(1:length(country_list), function(i) write.csv(country_list[[i]],
file = paste0(names(country_list[i]), ".csv"),
row.names = FALSE))
确保将工作目录设置为要保存文件的位置