修复功能,以便我可以输入多个单词

时间:2015-06-21 00:13:42

标签: r

我创建的东西可以计算输入的每个字母的值,输入是字母字符串,例如" back"或者"今天"或者你能想到的任何一个词。它还没有完成,但我想我会试着看看它是否只能处理一个单词输入。

这是代码:

compute_pos <- function(strg){

#store the middle position of the word
some_input <- nchar(strg)

if(!some_input%%2){

    result0 <- substr(strg, some_input/2, (some_input/2)+1)
}

else{

    odd_input <- median(sequence(some_input))

    result1 <- substr(strg, odd_input, odd_input)

}
#store first letter
split.wLB <- substr(strg, 1, 1)

#store last letter
split.wRB <- substr(strg, nchar(strg), nchar(strg))

matching_left <- letters[(which(split.wLB == letters))]

matching_center <- if ((nchar(strg) %% 2) != 0) {

    letters[(which(result1 == letters))]
}
else{
    result0[(which(result0 == result0))]
}

matching_right <- letters[(which(split.wRB == letters))]


print(paste("Values of the first letter of: ", strg))

#the value of the left most letter from the left most position
print(paste(matching_left,"L", 1))

#get the value of the left most letter from the mid position
if ((nchar(strg) %% 2) != 0) {

    print(paste( matching_left,"M", (.33333*1.00001) - ((odd_input - 1) * .05)))
}
else{
    print(paste( matching_left,"M", (.33333*1.00001) - (median(sequence(some_input)) - 1.5) * .05 ))
}

#get the value of the left most letter from the right most position
print(paste( matching_left,"R", (.33333*1.00001) - ((nchar(strg)- 1) * .05)))

print(paste("Values of the last letter of: ", strg))

#get the value of the right most letter from the left most position
print(paste( matching_right, "L", (.33333*1.00001) - ((nchar(strg)-1) * .05)))

#get the value of the right most letter from the mid most position
if ((nchar(strg) %% 2) != 0) {

    print(paste( matching_right, "M", (.33333*1.00001) - ((odd_input -1) * .05)))
}
else{
    print(paste( matching_right, "M", (.33333*1.00001) - (median(sequence(some_input)) - 1.5) * .05))
}

#get the value of the right most letter from the right most position
print(paste(matching_right, "R", 1))





}

当我测试出一个单词时,它运行正常,所以例如如果我要做compute_pos('back')输出是:

[1] "Values of the first letter of:  back"
[1] "b L 1"
[1] "b M 0.2833333333"
[1] "b R 0.1833333333"
[1] "Values of the last letter of:  back"
[1] "k L 0.1833333333"
[1] "k M 0.2833333333"
[1] "k R 1"

但是,如果我执行x <- c('back', 'today')然后compute_pos(x),我会收到以下错误:

Warning messages:
1: In if (!some_input%%2) { :
the condition has length > 1 and only the first element will be used
2: In if ((nchar(strg)%%2) != 0) { :
the condition has length > 1 and only the first element will be used
3: In if ((nchar(strg)%%2) != 0) { :
the condition has length > 1 and only the first element will be used
4: In if ((nchar(strg)%%2) != 0) { :
the condition has length > 1 and only the first element will be used

如何解决此问题,以便该功能可以将多个单词作为输入?

0 个答案:

没有答案