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?