我有一行代码需要每次运行12次并更换字符串。
unique1 <- unique(master[c("Country_University_1", "City_University_1", "Latitude_city_1", "Longitude_city_1")])
通过将每个列名末尾的数字递增1,这将生成12个单独的数据帧,然后我将与rbind
我的目标是编写一个函数,它将列名和的最后一个字符作为对象名称,并像x <- x + 1
一样递增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)
答案 0 :(得分:2)
重做以匹配请求。
Base = c("Country_University_", "City_University_", "Latitude_city_", "Longitude_city_")
output = rep(NULL,4)
for(i in 1:12) {
ColNames = sapply(Base, function(s) { paste(s, i, sep="") })
output=rbind(output, unique(master[ColNames]))
}
此代码未经过测试,因为我们没有掌握,但输出应该是您想要的。
答案 1 :(得分:1)
您需要在sub
使用一些正则表达式。看起来你想用越来越多的数字替换字符串末尾的数字,你可以检查一下,模式$
中匹配字符串结尾的位置,因此\\d+$
匹配数字末尾字符串:
for (i in 1:2) { print(sub("\\d+$", i, x)) }
#[1] "Country_University_1" "City_University_1" "Latitude_city_1"
#[4] "Longitude_city_1"
#[1] "Country_University_2" "City_University_2" "Latitude_city_2"
#[4] "Longitude_city_2"
根据您的更新,您可能需要这样的内容:
# get the number at the end of column names
number = sub(".*_(\\d+)$", "\\1", names(master))
# split the data frame by the number, rename each sub data frame and bind them together
do.call(rbind, lapply(split.default(master, number),
function(sdf) setNames(sdf, sub("_\\d+$", "", names(sdf)))))
答案 2 :(得分:0)
我们可以使用melt
中的data.table
轻松完成此操作,这可能需要多个measure
patterns
library(data.table)
melt(setDT(master), measure = patterns("^Country", "^City", "^Latitude", "^Longitude"),
value.name = c("Country_University", "City_University", "Latitude_city", "Longitude_city")
][, variable := NULL][]