我有一行代碼需要每次更新一次字符串才能運行12次。字符串替換時遞增1
unique1 <- unique(master[c("Country_University_1", "City_University_1", "Latitude_city_1", "Longitude_city_1")])
通過由一個在每個列名的末尾遞增的數字,這將產生12個獨立的dataframes我會然後用rbind
我的目標結合在一起是寫一個函數,它的最後列的角色名字和對象名稱和增量爲1像x <- x + 1
和12停止我一直在絕望嘗試寫看起來像
x <- c("Country_University_1", "City_University_1", "Latitude_city_1", "Longitude_city_1")
for (i in 1:12){
gsub("i", nchar(x[1:4]-1) x[1:4])
print(x)
}
功能
但我仍然不熟悉R語法。任何意見,將不勝感激
寫出全面,功能將執行以下操作:
unique1 <- unique(master[c("Country_University_1", "City_University_1", "Latitude_city_1", "Longitude_city_1")])
unique2 <- unique(master[c("Country_University_2", "City_University_2", "Latitude_city_2", "Longitude_city_2")])
unique3 <- unique(master[c("Country_University_3", "City_University_3", "Latitude_city_3", "Longitude_city_3")])
unique4 <- unique(master[c("Country_University_4", "City_University_4", "Latitude_city_4", "Longitude_city_4")])
unique5 <- unique(master[c("Country_University_5", "City_University_5", "Latitude_city_5", "Longitude_city_5")])
unique6 <- unique(master[c("Country_University_6", "City_University_6", "Latitude_city_6", "Longitude_city_6")])
unique7 <- unique(master[c("Country_University_7", "City_University_7", "Latitude_city_7", "Longitude_city_7")])
unique8 <- unique(master[c("Country_University_8", "City_University_8", "Latitude_city_8", "Longitude_city_8")])
unique9 <- unique(master[c("Country_University_9", "City_University_9", "Latitude_city_9", "Longitude_city_9")])
unique10 <- unique(master[c("Country_University_10", "City_University_10", "Latitude_city_10", "Longitude_city_10")])
unique11 <- unique(master[c("Country_University_11", "City_University_11", "Latitude_city_11", "Longitude_city_11")])
unique12 <- unique(master[c("Country_University_12", "City_University_12", "Latitude_city_12", "Longitude_city_12")])
output <- rbind(unique1, unique2, unique3, unique4, unique5, unique6, unique7, unique8, unique9, unique10, unique11, unique12)
謝謝,我編輯我的問題是一個小更精確。我已經手動編寫完整的代碼 - 我只是想知道我將來如何實現這一點。你如何在你的解決方案中包含'rbind'元素? –
'master'做什麼? – G5W
'master'是包含所有48個列的數據幀的名稱 –