hollyjolly
hollyjolly

Reputation: 115

How to use loop to make a stack in R?

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
}

Upvotes: 0

Views: 56

Answers (3)

akrun
akrun

Reputation: 887168

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)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388982

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)

Upvotes: 1

Schilker
Schilker

Reputation: 505

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

Upvotes: 1

Related Questions