循环R数据帧以生成条件字符串

时间:2014-07-28 02:14:22

标签: r

我有一个R数据帧,里面有11个代码。我需要运行循环或等效函数,并在每次迭代中动态生成一个字符串,每次迭代中最多有3个代码,用+号分隔。我怎么能这样做?

all_stockticks <-   
data.frame(symbol=c("AAPL","GOOG","MSFT","C","JPM","GS","INFY","WIT","CTSH","V","MA")
)

In iteration-1 output would be str=AAPL+GOOG+MSFT
In iteration-2 output would be str=C+JPM+GS
In iteration-3 output would be str=INFY+WIT+CTSH
In iteration-4 output would be str=V+MA

3 个答案:

答案 0 :(得分:1)

这是IRanges的方式:

library(IRanges)
symbolList <- relist(as.character(all_stockticks$symbol),
                     breakInChunks(nrow(all_stockticks), 3))
unstrsplit(symbolList, "+")

breakInChunks函数生成表示分区的对象。我们调用relist来形成具有此类分区的列表,最后unstrsplit(直观地说是strsplit的反转)来折叠字符串。这里,所有迭代都是本机代码,所以它应该很快。

答案 1 :(得分:0)

从动物园套餐中试用rollapply

library(zoo)
rollapply(all_stockticks, 3, by = 3, paste, collapse = "+", partial = TRUE, 
            align = "left")

,并提供:

     symbol          
[1,] "AAPL+GOOG+MSFT"
[2,] "C+JPM+GS"      
[3,] "INFY+WIT+CTSH" 
[4,] "V+MA"    

答案 2 :(得分:0)

您可以使用:

 n <- nrow(all_stockticks)
 c(by(all_stockticks[,1], as.numeric(gl(n,3,n)), FUN=paste,collapse="+"))
              1                2                3                4 
 #"AAPL+GOOG+MSFT"       "C+JPM+GS"  "INFY+WIT+CTSH"           "V+MA"