Am I using sapply incorrectly?

120 Views Asked by At

This code is suppose to take in a word, and compute values for letters of the word, based on the position of the letter in the word. So for a word like "broke" it's suppose to compute the values for the letter "r" and "k"

strg <- 'broke'   

#this part stores everything except the first,
#last, and middle position of the word

strg.leng <- nchar(strg)

other.letts <- sequence(strg.leng)

if (length(other.letts) %% 2 != 0) {

    oth_let1 <- other.letts[-c(1, ceiling(length(other.letts)/2), length(other.letts))]   

} else {

    oth_let0 <- other.letts[-c(1, c(1,0) + floor(length(other.letts)/2), length(other.letts))]

}

print(paste("Values of the other letters of: ", strg))

#here is where the computation starts, taking in the objects created above

if ((nchar(strg) %% 2) != 0) {

    sapply(oth_let1, function(i) print(paste(oth_let1[i], "L", (.66666*1.00001) - (oth_let1[i] - 1) *.05 )))

} else {

    sapply(oth_let0, function(i) print(paste(oth_let0[i], "L", (.66666*1.00001) - (oth_let0[i] - 1) *.05 )))

}

However for "broke" I get this which is only computing the value of "k" and some other stuff:

[1] "4 L 0.5166666666"
[1] "NA L NA"
[1] "4 L 0.5166666666" "NA L NA" 

While the desired output should be a value for both "r" and "k", so something like:

[1] "2 L 0.61666666"
[1] "4 L 0.51666666" 

What am I doing wrong? Am I using sapply incorrectly?

2

There are 2 best solutions below

0
On BEST ANSWER

sapply iterates through the supplied vector or list and supplies each member in turn to the function. In your case, you're getting the values 2 and 4 and then trying to index your vector again using its own values. Since the oth_let1 vector has only two members, you get NA. You could fix your current code by replacing the oth_let1[i] with just i. However, your code could be greatly simplified to:

strg <- 'broke'   
lets <- 2:(nchar(strg) - 1)
lets <- lets[-(1:2 + length(lets)) / 2] # removes middle item for odd and middle two for even
cat("Values of the other letters of:", strg, "\n")
#here is where the computation starts, taking in the objects created above
writeLines(paste(lets, "L", 0.66666*1.00001 - (lets - 1) * 0.05, sep = " "))

I'm assuming you want to output the results to the console.

0
On

You're using sapply correct, what you're getting wrong is the function inside it. What you want is the i element of the other.letts variable, not from the oth_let1. oth_let1 have the indexes from the other.letts.

The code bellow should work, I also change the name of the variable to oth_let, so you don't have to use other if. For the output be exact what you ask for I used the invisible function.

strg <- 'broke'   
strg.leng <- nchar(strg)

other.letts <- sequence(strg.leng)

if(length(other.letts) %% 2 != 0) {
  oth_let <- other.letts[-c(1, ceiling(length(other.letts)/2),
                        length(other.letts))]   
}else{
  oth_let <- other.letts[-c(1, c(1,0) + floor(length(other.letts)/2),
                        length(other.letts))]
}

print(paste("Values of the other letters of: ", strg))

invisible(sapply(oth_let, 
                 function(i) 
                 print(paste(other.letts[i], "L", (.66666*1.00001) - (other.letts[i] - 1) *.05 ))))