我有以下数据:在R中,我想用“广告”替换列名匹配
这是column关键字的元素。我要重复执行此过程以复制100列。此过程完成后。我想删除100列中的每六列(命中后)。
这是下面给出的数据样本的可复制代码;
hits<- c(89, 93, 97, 86, 82, 83, 84, 76, 87, 87) geo <- c("ca","ca","ca","ca","ca","ca","ca","ca","ca","ca") time<- c("all","all","all","all","all","all","all","all","all","all")
keyword<- c("adver","adver","adver","adver","adver","adver","adver","adver","adver","adver"
)
group<- c("web","web","web","web","web","web","web","web","web","web")
category<- c(12,12,12,12,12,12,12,12,12,12) subject1 <- list(hits = hits,geo = geo,time = time, keyword = keyword, group = group) pt_data <- data.frame(subject1, hits, geo,time, keyword, group, category, stringsAsFactors = FALSE)
我想要用adver更改列名“ hits”,adver是“关键字”列中的项目,此过程将重复100列左右。 然后分别删除100列左右的列,地理位置,时间,关键字和组。 请注意,点击数,地理位置,时间,关键字,组,类别从点击数开始再次重复,…………..
答案 0 :(得分:0)
joshpk是正确的,您的问题应包括可重现的代码示例,以便我们可以看到您遇到的确切问题。还是,我认为这是您要寻找的?
#creating example dataset
x <- data.frame(matrix(ncol=100, nrow=1))
x[1,] <- 1:100
colnames(x) <- paste0("r",1:100)
#colnames part
colnames(x)[seq(4,100,6)] <- x[1,seq(4,100,6)]
#deleting columns part
y <- x[,-(seq(5,100,6))]
#result
y[1,1:6]
r1 r2 r3 4 r6 r7
1 2 3 4 6 7