简体   繁体   中英

How can i add a column based on some condition quickly in R?

I have a dataframe that contains one column and i want to make another column based on some condition in the first column. Here is my script that i have written so far and it works but it is very slow since it has around 50k rows.

 data <- read.table("~/Documents/git_repos/Aspen/Reference_genome/Potrs01-genome_mod_id.txt")
> dim(data) # [1] 509744      1
> head(data)
           V1
1 Potrs000004
2 Potrs000004
3 Potrs000004
4 Potrs000004
5 Potrs000004
6 Potrs000004

test <- paste("Potrs00000", seq(000001,10000,by=1), sep ="")
length(test) # [1] 10000
> head(test)
[1] "Potrs000001" "Potrs000002" "Potrs000003" "Potrs000004" "Potrs000005"
[6] "Potrs000006"

test.m <- matrix("NA", nrow = 509744, ncol = 2 )
dim(test.m) # [1] 509744      2
> head(test.m)
     [,1] [,2]
[1,] "NA" "NA"
[2,] "NA" "NA"
[3,] "NA" "NA"
[4,] "NA" "NA"
[5,] "NA" "NA"
[6,] "NA" "NA"

 for (i in test) {
   for (j in data$V1) {
     if (i == j)
       test.m[,1] = j
       test.m[,2] = "chr9"
      }
    }
test.d <- as.data.frame(test.m)
> head(test.d)
           V1   V2
1 Potrs000004 chr9
2 Potrs000004 chr9
3 Potrs000004 chr9
4 Potrs000004 chr9
5 Potrs000004 chr9
6 Potrs000004 chr9

Is there a way to modify the code to speed it up?

It seems like you want the values of V1 from data which match an element in test .

I would do this with data.table :

library(data.table)
setDT(data)
data[,.(V1[V1 %in% test], "chr9")]

Note that the result is already a data.table (which is also a data.frame )

Sample Data

set.seed(10239)
data<-data.frame(V1=sample(c(test[1:10],LETTERS[1:10]),10))
> data
            V1
1            D
2            A
3            E
4  Potrs000006
5  Potrs000001
6  Potrs000007
7  Potrs000008
8  Potrs000003
9            B
10 Potrs000002
setDT(data)
> data[,.(V1[V1 %in% test], "chr9")]
            V1   V2
1: Potrs000006 chr9
2: Potrs000001 chr9
3: Potrs000007 chr9
4: Potrs000008 chr9
5: Potrs000003 chr9
6: Potrs000002 chr9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM