我是R中的新手,并且必须计算一个操作的示例,该操作在单个向量中出现某个字符串后生成一个新行。
此处说明了矢量:
address_list <- c("Road","Number","City","Zipcode","Telephone","House","Road","Number","City","Zipcode","House","Road","Number","City","Zipcode","Telephone","House")
操作是每次出现“House”时都会创建一个新行。将矢量留在矩阵中:
Road,Number,City,Zipcode,Telephone,House
road,Number,City,Zipcode,,House
road,Number,City,Zipcode,Telephone,House
我对Excel或VBA一无所知。但我可以想象这个问题与我在R中构建的操作有点相似。
我想出了一些伪代码,可能会给出一个更直观的例子,告诉我如何思考以解决这个问题。
gsub(list, \s, ",")
For
every "House" in list as i
rbind(list, \n, i)
答案 0 :(得分:2)
我们从向量中获取unique
个元素(&#39; address_list&#39;),循环遍历这些元素,然后在&#39; address_list&#39;中提取元素。 (或使用split
,即lst <- split(address_list, address_list)
),在list
元素的末尾填充NA,length
小于最大length
,cbind
它使用matrix
创建的序列创建paste
(&#39; m1&#39;)和ave
。
lst <- lapply(unique(address_list), function(x) address_list[address_list==x])
m1 <- do.call(cbind, lapply(lst, `length<-`, max(lengths(lst))))
m1[] <- ifelse(is.na(m1), NA, paste0(m1, ave(m1, m1, FUN = seq_along)))
m1
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] "Road1" "Number1" "City1" "Zipcode1" "Telephone1" "House1"
#[2,] "Road2" "Number2" "City2" "Zipcode2" "Telephone2" "House2"
#[3,] "Road3" "Number3" "City3" "Zipcode3" NA "House3"