将每个值放在表中

时间:2015-08-25 20:54:03

标签: r

我有一个r代码,我打印值并对其进行小型计算。

s ='abchae'
currentlen = 0
longestlen = 0
current = ''
longest = ''
alphabet = 'abcdefghijklmnopqrstuvwxyz'

for char in s:

    for number in range(0,len(s)):
    if s[number] == char:
        n = number 

    nxtchar = 1        
    alphstring = s[n] 
    while alphstring in alphabet == True and n+nxtchar <= 5:
        alphstring += s[n+nxtchar]
        nxtchar += 1
        currentlen = len(alphstring)
        current = alphstring

        if currentlen > longestlen:
            longest = current

print longest 

这打印出i的每个值。如何将这些值放入循环内的向量中? 我试过这段代码,但它只给出了最后的值

    for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
    i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
     }

1 个答案:

答案 0 :(得分:0)

您可以将for循环更改为以下内容:

D = numeric(0)
for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
    i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
    D <- c(D, i)
} 

但事实证明,在R中的for循环中增长一个对象非常慢。您可能甚至不需要for循环。您可以尝试利用R的矢量化:

x <- sample.data1$SENTIMENT_STRENGTH_91D

D <- (x - max(x))/C