以特定格式将数据写入文本文件

时间:2018-08-21 12:33:22

标签: r loops

我有一个.csv,其中包含一个国家/地区的所有省份,地区和城镇。我想获取这些数据,并将其处理到一个文本文件中,每个城市一行,而有关该城市的所有信息均以以下特定格式设置:

[1] CountryName<Country~Sovereign State>|[ProvinceName1]<Province~Governorate>|[Districtname1]<District>[Cityname1]<City
[2] CountryName<Country~Sovereign State>|[ProvinceName1]<Province~Governorate>|[Districtname1]<District>[Cityname2]<City
[3] CountryName<Country~Sovereign State>|[ProvinceName1]<Province~Governorate>|[Districtname1]<District>[Cityname3]<City
...
[56] CountryName<Country~Sovereign State>|[ProvinceName2]<Province~Governorate>|[Districtname1]<District>[Cityname1]<City

我已经尝试过使用paste()和各自的列名以及首先输出write.table的for循环,但是这些列的顺序不符合我想要的顺序。

1 个答案:

答案 0 :(得分:1)

请参见以下代码。您可以将apply函数与MARGIN = 1(按行)一起使用,以paste0的行元素成一行:

# data simulation
nms <- c("CountryName", "Country_Sovereign_State", "ProvinceName", "Province_Governorate", "Districtname", "District", "Cityname")
df <- data.frame(t(sapply(1:7, function(x) paste0(nms, x))), stringsAsFactors = FALSE)

# create data character vector
res <- apply(df, 1, function(x) paste0(x[1], "<", x[2], ">|[", x[3], "]<", x[4],
                                       ">|[", x[5], "]<", x[6],">[", x[7], "]"))

res

输出:

[1] "CountryName1<Country_Sovereign_State1>|[ProvinceName1]<Province_Governorate1>|[Districtname1]<District1>[Cityname1]"
[2] "CountryName2<Country_Sovereign_State2>|[ProvinceName2]<Province_Governorate2>|[Districtname2]<District2>[Cityname2]"
[3] "CountryName3<Country_Sovereign_State3>|[ProvinceName3]<Province_Governorate3>|[Districtname3]<District3>[Cityname3]"