R with padding a vector using for-loop

I iterate over a vector, for each element I look through something in the table named rowname and copy the return to another vector. For this

The following code is used:
gs1 = function(p) { output <- character() #empty vector to which results will be forwarded for (i in 1:length(p)) { test <- p[i] index <- which(rownames(conditions) == test) toappend <- conditions[index,3] #working output[i] <- toappend print(paste(p[i],index,toappend,output[i])) } return(output) } 

All he spits out is a vector with numbers .... while all the other variables seem to contain the correct information (as checked by the print function) I feel like I'm doing something terribly wrong in filling out the output vector ... I could also use

 output <- c(output,toappend) 

But it gives me exactly the same, wrong and strange conclusion.

All help is much appreciated!

Output example

 > gs1 = function(p) + { + output <- character() #empty vector to which results will be pasted + + for (i in 1:length(p)) { + test <- p[i] + index <- which(rownames(conditions) == test) + toappend <- conditions[index,3] #working + + output <- c(output,toappend) + output[i] <- toappend + print(paste(p[i],index,toappend,output[i],sep=",")) + } + return(output) + } > ########################### > test <- colnames(tri.data.1) > gs1(test) [1] "Row.names,,,NA" [1] "GSM235482,1,Glc A,5" [1] "GSM235484,2,Glc A,5" [1] "GSM235485,3,Glc A,5" [1] "GSM235487,4,Xyl A,21" [1] "GSM235489,5,Xyl A,21" [1] "GSM235491,6,Xyl A,21" [1] "GSM297399,7,pH 2.5,12" [1] "GSM297400,8,pH 2.5,12" [1] "GSM297401,9,pH 2.5,12" [1] "GSM297402,10,pH 4.5,13" [1] "GSM297403,11,pH 4.5,13" [1] "GSM297404,12,pH 4.5,13" [1] "GSM297563,13,pH 6.0,14" [1] "GSM297564,14,pH 6.0,14" [1] "GSM297565,15,pH 6.0,14" [1] "5" "5" "5" "5" "21" "21" "21" "12" "12" "12" "13" "13" "13" "14" "14" "14" 
+4
source share
2 answers

Most likely you are using a data frame, not a table, and most likely your third column is not a character vector, but a factor. And there is no need to write this function, you can easily get the required:

 conditions[X,3] 

where X is a character vector of string names. eg:

 X <- data.frame( var1 = 1:10, var2 = 10:1, var3 = letters[1:10], row.names=LETTERS[1:10] ) > test <- c("F","D","A") > X[test,3] [1] fda Levels: abcdefghij 

To get it in characters:

 > as.character(X[test,3]) [1] "f" "d" "a" 
+5
source

[Joris’s remarks suggest that I was too cryptic, so some further explanation]:

Effectively, if we ignore processing in your loop, this is what you have:

 > p <- 1:10 > gs1 <- function(p) { + output <- character() + for(i in seq_along(p)) { + output[i] <- p[i] * 10 + print(output) + } + return(output) + } > foo <- gs1(p) [1] "10" [1] "10" "20" [1] "10" "20" "30" [1] "10" "20" "30" "40" [1] "10" "20" "30" "40" "50" [1] "10" "20" "30" "40" "50" "60" [1] "10" "20" "30" "40" "50" "60" "70" [1] "10" "20" "30" "40" "50" "60" "70" "80" [1] "10" "20" "30" "40" "50" "60" "70" "80" "90" [1] "10" "20" "30" "40" "50" "60" "70" "80" "90" "100" > foo [1] "10" "20" "30" "40" "50" "60" "70" "80" "90" "100" 

So, gs1 returns something, and the output fills up while the toappend is characteristic or can be forced into a character to go to output . Now, if toappend not what you think, this is where you will begin to experience problems.

I see two potential problems; i) toappend is actually a factor (which Joris also mentions), and you get the numerical equivalent of internal coding for this level. In this case

 ouput[i] <- as.character(toappend) 

should be enough, or ii) index longer than 1, and you get more elements in the vector that you expect, and therefore, at the next iteration, you overwrite them.

Are you a sure toappend single character character of length 1? How about you showing us the wrong conclusion (edit your question and add the output from the function) and let us know why this is wrong!

Of course, this can be simplified to conditions[p, 3] and there is no need for a loop, but I assume that your actual functions are more complex?


Note on setting up cycles

As for loops in general, you make a mistake by not reallocating memory. You should not do as you are. Note that at each iteration of R, output should grow by one element per iteration. The same can be said about your idiom output <- c(output, toappend) . This is due to the large amount of redundant copying of the vector, which the swamp goes down. Instead, allocate enough storage space and fill out the output as you do. For instance:.

 gs2 <- function(p) { output <- character(length = length(p)) for(i in seq_along(p)) { output[i] <- p[i] * 10 print(output) } return(output) } 

which produces this conclusion:

 > gs2(p) [1] "10" "" "" "" "" "" "" "" "" "" [1] "10" "20" "" "" "" "" "" "" "" "" [1] "10" "20" "30" "" "" "" "" "" "" "" [1] "10" "20" "30" "40" "" "" "" "" "" "" [1] "10" "20" "30" "40" "50" "" "" "" "" "" [1] "10" "20" "30" "40" "50" "60" "" "" "" "" [1] "10" "20" "30" "40" "50" "60" "70" "" "" "" [1] "10" "20" "30" "40" "50" "60" "70" "80" "" "" [1] "10" "20" "30" "40" "50" "60" "70" "80" "90" "" [1] "10" "20" "30" "40" "50" "60" "70" "80" "90" "100" [1] "10" "20" "30" "40" "50" "60" "70" "80" "90" "100" 

The duplicated last line is associated with the automatic printing of the object ( output ) returned by the function.

+2
source

Source: https://habr.com/ru/post/1332339/


All Articles