How to use loop to make a stack in R?

create raster stack in r
for loop stack overflow java

I'm a newbie to this world. I am currently working with R codes to analyze some sequencing data and just stuck now.

Here's some problem description.

What I'd like to do is to select first word of $v3 from pat1_01_exonic data(115 rows)and make it file. (I used strsplit function for this)

till now, I tried below code 1 attached, and it worked for first line. but the problem is I can't do this for 115 times.

so, It seems like a loop is necessary.

I'm not really confident with making a loop by myself. and as I expected it didn't work.

for making stack I thought about using append or rbind or stack.

Can anyone give me some advice about how to fix this problem? Big thanks in advance

#code1
pat1_01_exonic$V3 <-as.character(pat1_01_exonic$V3)
pat1 <- data.frame(head(strsplit(pat1_01_exonic$V3, ":")[[1]],1))

#code2
for (i in 1: nrow(pat1_01_exonic)) {
  pat1_output <- vector()
  sub[i] <- data.frame(head(strsplit(pat1_01_exonic$V3, ":")[[i]],1))
  pat1_0utput <- append(sub[i])
  i <- i+1
}

Many of the times, you can avoid for loop in R. If I have understood you correctly, here you can use sub to get first string before ":"

pat1_01_exonic$new_col <- sub(":.*", "", pat1_01_exonic$V3)
pat1_01_exonic

#            V3 new_col
#1  abc:def:avd     abc
#2     afd:adef     afd
#3 emg:rvf:temp     emg

data

pat1_01_exonic <- data.frame(V3 = c("abc:def:avd", "afd:adef", "emg:rvf:temp"), 
                  stringsAsFactors = FALSE)

Looping a raster stack with a step, R, I have tried to do this with the following loop but I get Error in compareRaster(x, value) : different extent. What is wrong with my code? new<-stack() for  For example, solutions that make use of loops are less efficient than vectorized solutions that make use of apply functions, such as lapply and sapply. It’s often better to use the latter. Nevertheless, as a beginner in R, it is good to have a basic understanding of loops and how to write them.

The below code is an an example to create a new variable "V3_First_Word" that selects the first word in the original string.

Want<-pat1_01_exonic%>%
    mutate(V3_First_Word=word(V3,1,1)) # This creates new varaible and selects first word

Make a raster stack from a loop in R : rstats, An advantage of this is you can use purrr::map rather than a loop, as it will output a list. Accessing each raster in stack with loop using R. Ask Question Asked 6 years, 7 months ago. Active 6 months ago. Viewed 10k times 2. 3. I have a stack of rasters and

In base R, we can use read.table

pat1_01_exonic$new_col <- read.table(text = pat1_01_exonic$V3, sep=":",
   header = FALSE, fill = TRUE, stringsAsFactors = FALSE)[,1]
pat1_01_exonic$new_col
#[1] "abc" "afd" "emg"

Or strsplit and select the first element

sapply(strsplit(pat1_01_exonic$V3, ":"), `[`, 1)
data
pat1_01_exonic <- data.frame(V3 = c("abc:def:avd", "afd:adef", "emg:rvf:temp"), 
              stringsAsFactors = FALSE)

Logic and Theory of Algorithms: 4th Conference on Computability in , Loop: push 1; %% marker to make stack non-empty push 0; %% try 0 as first Let I : θ → ω,R : θ → (ωω) be the computation by P with oracle Z and trivial input  In the code block, you can use the identifier. Each time R loops through the code, R assigns the next value in the vector with values to the identifier. Calculate values in a for loop. Let’s take another look at the priceCalculator() function. Earlier, we show you a few possibilities to adapt this function so you can apply a different VAT

Introducing “The Loop”: A Foundation in Listening, You can give us your thoughts on what you'd like to see us do by filling out this survey: Through the Loop[closed for responses 1/25/20]. We'll  I've never done loops in R before, so far I have: for (i in 1:2) {lm(Yield~Practice[i]*Treatment)} How do I adjust this so that it calls just the first level of Practice (which is PL) in the first iteration of the loop and then the second level(LT) in the second iteration? Then I'd like to write the anova() into the loop as well.

Batch raster calculation in R using a loop, by using a function to loop through a list of folders and add together the within each folder using the sum function on a raster stack object. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Plot many histograms using a for loop in R

Return Stack Tutorial - Gforth Manual, >r takes an element from the data stack and pushes it onto the return stack; Forth programmers usually use the return stack for storing data temporarily, if using that you have pushed on the return stack in a colon definition or counted loop,  You can use > dates <- seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by=1) to generate a vector of consecutive days. What you want to do with this is not entirely clear from your pseudo-code, but you can iterate directly over the vector (which is generally not what you want in R)

Comments
  • Thanks a lot! but It seems like I have issues with the data type. I got an error alarm saying "Error in strsplit(pat1_01_exonic$V3, ":") : non-character argument"
  • @sunho strsplit takes only character and not factor if your column is factor, convert it to character with as.character