Answers.Body: <p>This explicitly doesn't use <code>apply()</code> or <code>lapply()</code>, but rather the <code>dplyr</code> package. This is probably easier to read and debug:</p>
  
  <pre class="lang-r prettyprint-override"><code>library("dplyr")
  snp_ids %&gt;% mutate(window_15000=as.numeric(paste(chr, ceiling(pos/15000), sep=".")),
                     window_30000=as.numeric(paste(chr, ceiling(pos/30000), sep=".")),
                     window_50000=as.numeric(paste(chr, ceiling(pos/50000), sep=".")),
                     window_100000=as.numeric(paste(chr, ceiling(pos/100000), sep=".")))
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-21T11:24:44.207Z
Answers.Text: This is a bit manual, don't you think?
Answers.UserDisplayName: zx8754
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-21T11:26:06.430Z
Answers.Text: Manual and clear is easier to debug and understand 6 months later (one could also put most of that in a function to make it shorter).
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-21T11:45:14.063Z
Answers.Text: Of course, personal preferences. But if there were 100 windows, that change by 500 bases for each window in 6 month time.
Answers.UserDisplayName: zx8754
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-22T16:24:12.503Z
Answers.Text: Manual isn’t the problem — having duplication is. If I had to write this code, five times out of ten (at least!) I’d make a typo in at least one of the numbers, leading to mismatches between the column names and their actual values. *That’s why* we automate. (I’m saying “I” but what I really mean is “the average, competent programmer”; this class of mistakes is incredibly common).
Answers.UserDisplayName: Konrad Rudolph
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-22T16:45:59.927Z
Answers.Text: A couple lines of code duplication is a nonissue. If someone neede to add a hundred columns then sure, but for three then trivial intelligibility is more useful.
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-21T09:24:34.490Z
Answers.LastActivityDate: 2017-11-21T09:24:34.490Z
Answers.UserDisplayName: Devon Ryan
Answers.Body: <p>Not clear what we are trying to do, but here is the solution using <code>lapply</code> that should replicate your existing <code>forloop</code>.</p>
  
  <pre><code># example input data
  snp_ids &lt;- read.table(text = "
  snp_id  chr  pos
  Chr01__912   1  912
  Chr01__944   1  944
  Chr01__1107  1 1107
  Chr01__1118  1 1118
  Chr01__1146  1 1146
  Chr01__1160  1 1160", header = TRUE)
  
  # window sizes to loop through
  win_size &lt;- c(15000, 30000, 50000, 100000)
  
  res &lt;- cbind(snp_ids,
               data.frame(
                 lapply(setNames(win_size, paste("window", win_size, sep = "_")), function(w)
                   as.numeric(paste(snp_ids$chr, ceiling(snp_ids$pos/w), sep = "."))
                 )))
  
  # result
  res
  #        snp_id chr  pos window_15000 window_30000 window_50000 window_1e.05
  # 1  Chr01__912   1  912          1.1          1.1          1.1          1.1
  # 2  Chr01__944   1  944          1.1          1.1          1.1          1.1
  # 3 Chr01__1107   1 1107          1.1          1.1          1.1          1.1
  # 4 Chr01__1118   1 1118          1.1          1.1          1.1          1.1
  # 5 Chr01__1146   1 1146          1.1          1.1          1.1          1.1
  # 6 Chr01__1160   1 1160          1.1          1.1          1.1          1.1
  </code></pre>
  
  <hr>
  
  <p><strong>Edit:</strong> If we really want to use <code>forloop</code>, then try below:</p>
  
  <pre><code>for (i in win_size){
    windows &lt;- ceiling(snp_ids$pos/i)
    snp_ids[, paste0("window_", i)] &lt;- as.numeric(paste(snp_ids$chr, windows, sep = "."))
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-21T20:12:02.383Z
Answers.Text: thanks so much @zx8754. I have edited my post a bit and hope it is more understandable. Your code worked for me. But, as I am learning R programming I wanted to know is there any other way that I could fix my own code above? Thanks so much for your help.
Answers.UserDisplayName: Anna1364
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-21T21:43:09.553Z
Answers.Text: @Anna1364 See edit, yes we could use `forloop` but I prefer `apply` family.
Answers.UserDisplayName: zx8754
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-21T08:38:04.667Z
Answers.LastActivityDate: 2017-11-21T21:42:34.017Z
Answers.LastEditDate: 2017-11-21T21:42:34.017Z
Answers.UserDisplayName: zx8754
Body: <p>I am trying to assign different window sizes to my SNPs dataset to identify regions under selection.</p>
  
  <p>this is the head of my data</p>
  
  <pre><code>head(snp_ids)
     snp_id   chr  pos
  Chr01__912    1  912
  Chr01__944    1  944
  Chr01__1107   1 1107
  Chr01__1118   1 1118
  Chr01__1146   1 1146
  Chr01__1160   1 1160
  
  class(snp_ids)
  data.frame
  </code></pre>
  
  <p>I have chosen 4 different window sizes, <code>win_size &lt;- c(15000, 30000, 50000, 100000)</code>.
  I have assigned each of these different window sizes to my snp_ids dataset to identify how many SNPs are distributed within each window by looping through each window size </p>
  
  <pre><code>for (i in 1:length(win_size)){
    windows &lt;- sapply(snp_ids$pos, function(x) (ceiling(x/win_size[i])))
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-20T20:07:17.667Z
Comments.Text: I would like to help, but do not completely follow. I am not sure what you're trying to do. Furthermore, if you want help with code, you should provide a minimal data set to work with (for us to test it).
Comments.UserDisplayName: benn
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-21T08:27:24.017Z
Comments.Text: How are you getting `1.2` for window_30000?
Comments.UserDisplayName: zx8754
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-30T08:49:33.477Z
Comments.Text: @Anna To help others you can post your own answer to the question. But please do not include it in the question body (unless they are unsuccessful attempts)
Comments.UserDisplayName: llrs
Tags: <r>
Title: Use paste function with apply families
ThreadText: # Use paste function with apply families
  
  (Asked by: username_0 on 2017-11-20)
  
  I am trying to assign different window sizes to my SNPs dataset to identify regions under selection.
  
  this is the head of my data
  
  ```
  head(snp_ids)
     snp_id   chr  pos
  Chr01__912    1  912
  Chr01__944    1  944
  Chr01__1107   1 1107
  Chr01__1118   1 1118
  Chr01__1146   1 1146
  Chr01__1160   1 1160
  
  class(snp_ids)
  data.frame
  
  ```
  
  I have chosen 4 different window sizes, `win_size <- c(15000, 30000, 50000, 100000)`.
  I have assigned each of these different window sizes to my snp\_ids dataset to identify how many SNPs are distributed within each window by looping through each window size
  
  ```
  for (i in 1:length(win_size)){
    windows <- sapply(snp_ids$pos, function(x) (ceiling(x/win_size[i])))
  }
  
  ```
  
  **Tags:** r
  
  > **Comments:**
  > **username_1**: I would like to help, but do not completely follow. I am not sure what you're trying to do. Furthermore, if you want help with code, you should provide a minimal data set to work with (for us to test it).  
  > **username_2**: How are you getting `1.2` for window_30000?  
  > **username_3**: @Anna To help others you can post your own answer to the question. But please do not include it in the question body (unless they are unsuccessful attempts)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2017-11-21. Score: 4 | Accepted answer
  
  Not clear what we are trying to do, but here is the solution using `lapply` that should replicate your existing `forloop`.
  
  ```
  # example input data
  snp_ids <- read.table(text = "
  snp_id  chr  pos
  Chr01__912   1  912
  Chr01__944   1  944
  Chr01__1107  1 1107
  Chr01__1118  1 1118
  Chr01__1146  1 1146
  Chr01__1160  1 1160", header = TRUE)
  
  # window sizes to loop through
  win_size <- c(15000, 30000, 50000, 100000)
  
  res <- cbind(snp_ids,
               data.frame(
                 lapply(setNames(win_size, paste("window", win_size, sep = "_")), function(w)
                   as.numeric(paste(snp_ids$chr, ceiling(snp_ids$pos/w), sep = "."))
                 )))
  
  # result
  res
  #        snp_id chr  pos window_15000 window_30000 window_50000 window_1e.05
  # 1  Chr01__912   1  912          1.1          1.1          1.1          1.1
  # 2  Chr01__944   1  944          1.1          1.1          1.1          1.1
  # 3 Chr01__1107   1 1107          1.1          1.1          1.1          1.1
  # 4 Chr01__1118   1 1118          1.1          1.1          1.1          1.1
  # 5 Chr01__1146   1 1146          1.1          1.1          1.1          1.1
  # 6 Chr01__1160   1 1160          1.1          1.1          1.1          1.1
  
  ```
  
  ---
  
  **Edit:** If we really want to use `forloop`, then try below:
  
  ```
  for (i in win_size){
    windows <- ceiling(snp_ids$pos/i)
    snp_ids[, paste0("window_", i)] <- as.numeric(paste(snp_ids$chr, windows, sep = "."))
  }
  
  ```
  
  > **Comments:**
  > **username_0**: thanks so much @username_2. I have edited my post a bit and hope it is more understandable. Your code worked for me. But, as I am learning R programming I wanted to know is there any other way that I could fix my own code above? Thanks so much for your help.  
  > **username_2**: @username_0 See edit, yes we could use `forloop` but I prefer `apply` family.  
  
  ---
  
  ### Answer by username_4 on 2017-11-21. Score: 2
  
  This explicitly doesn't use `apply()` or `lapply()`, but rather the `dplyr` package. This is probably easier to read and debug:
  
  ```r
  library("dplyr")
  snp_ids %>% mutate(window_15000=as.numeric(paste(chr, ceiling(pos/15000), sep=".")),
                     window_30000=as.numeric(paste(chr, ceiling(pos/30000), sep=".")),
                     window_50000=as.numeric(paste(chr, ceiling(pos/50000), sep=".")),
                     window_100000=as.numeric(paste(chr, ceiling(pos/100000), sep=".")))
  
  ```
  
  > **Comments:**
  > **username_2**: This is a bit manual, don't you think?  
  > **username_4**: Manual and clear is easier to debug and understand 6 months later (one could also put most of that in a function to make it shorter).  
  > **username_2**: Of course, personal preferences. But if there were 100 windows, that change by 500 bases for each window in 6 month time.  
  > **username_5**: Manual isn’t the problem — having duplication is. If I had to write this code, five times out of ten (at least!) I’d make a typo in at least one of the numbers, leading to mismatches between the column names and their actual values. *That’s why* we automate. (I’m saying “I” but what I really mean is “the average, competent programmer”; this class of mistakes is incredibly common).  
  > **username_4**: A couple lines of code duplication is a nonissue. If someone neede to add a hundred columns then sure, but for three then trivial intelligibility is more useful.  
  
  ---
  
  