Replace for loop - R Script


Replace for loop - R Script



Is there any way to replace the For loop in the code below by Apply family function or represent it in any other way to speed up the code execution:


For loop


Apply


line = strsplit(textFileContent, " ")
for(i in 2:n){ #looping from 2nd line to nth line of an input txt file

a = as.numeric(line[[i]][1]) #starting index in a matrix
b = as.numeric(line[[i]][2]) #ending index in a matrix
k = as.numeric(line[[i]][3]) #a number

mat[1,a:b] = mat[1,a:b] + k
#mat is a matrix of 5 zeros.
#mat = [0, 0, 0, 0, 0].
#Each time a value (k) will be added to all the elements between starting and ending index
}



EDIT



Sample input(textFileContent):



5 3



1 2 100



2 5 100



3 4 100



Here, 1st line of the input contains 2 space separated number. 1st number says that the mat matrix will initially contain 5 zeros. 2nd number says how many lines follow the first line. In the above code it is taken as n.


mat


n



Next lines contain 3 space separated numbers. 1st is a, 2nd is b and the 3rd is k.


a


b


k



N.B.: The values of a, b, k, n and 1st line's 1st number may vary.



Output:



[100, 200, 200, 200, 100]



Modified code


filepath = "C:textFileContent.text"
con = file(filepath, "r")
inp1 = readLines(con, n = -1, warn = FALSE)

line = strsplit(inp1, " ")

matadd<- function(line) {
a<-as.numeric(line[1])
b<-as.numeric(line[2])
k<-as.numeric(line[3])
mat[1,a:b]<<-mat[1,a:b]+k
}
mat<-matrix(0,nrow=1,ncol=as.numeric(line[[1]][1]))
temp<-sapply(line[2:(as.numeric(line[[1]][2])+1)],matadd)
cat(apply(mat, 1, max))
close(con)



With the sample input provided above the line looks like this:


line


> line
[[1]]
[1] "5" "3"

[[2]]
[1] "1" "2" "100"

[[3]]
[1] "2" "5" "100"

[[4]]
[1] "3" "4" "100"



Thanks in advance.





Please provide sample data for textFileContent. Also what's value of n are you using in above for-loop?
– MKR
Jun 29 at 18:02


textFileContent


n


for-loop





Not sure what you're trying to achieve here, but this will probably start you off: sapply(line,"[",1:3) This will give you a matrix where the first row is your a, the second your b and the third your k (you might want to transpose it using t()).
– doviod
Jun 29 at 18:10



sapply(line,"[",1:3)


t()





Please see the Edit section for sample data.
– Leo
Jun 29 at 18:41





I updated my answer with a fuller solution.
– doviod
Jun 29 at 18:48





Did you make an error in your sample output? Shouldn't it be 100, 200, 200, 200, 100? column 5 gets added 100 only in the second round...
– doviod
Jun 29 at 18:57




1 Answer
1



OK, this is a guess, but if I got your request and your data structure correctly, this should work:


matadd<- function(line) {
a<-as.numeric(line[1])
b<-as.numeric(line[2])
k<-as.numeric(line[3])
mat[1,a:b]<<-mat[1,a:b]+k
}
# Create the matrix with all zeros
mat<-matrix(0,nrow=1,ncol=as.numeric(line[[1]][1]))
# sapply the function on rows 2 thru end of the list
sapply(line[2:(as.numeric(line[[1]][2])+1)],matadd)



actually I could've just used the length of the list, come to think of it...


length



For example, with line<-list(c(5,2),c(2,3,5),c(1,4,2)), this is the result:


line<-list(c(5,2),c(2,3,5),c(1,4,2))



mat


[,1] [,2] [,3] [,4] [,5]

[1,] 2 7 7 2 0





I have modified my code with what you mentioned but it is getting runtime error for some cases. However, I don't have the access to check the inputs for which it is getting runtime exception.
– Leo
Jun 29 at 19:06





Will be hard to figure this out without a sample file. Can you share what the first few items on line look like?
– doviod
Jun 29 at 19:10



line





Updated the question.
– Leo
Jun 29 at 19:14





What are you trying to do in the 'cat' line? With the example you provided, my code works fine on my system.
– doviod
Jun 29 at 22:57





The web portal where I am executing the code does not pass the code if the output has the auto generated row index number. For example, [1] 200 is treated wrong, it should be only 200. Hence I am using cat.
– Leo
Jun 30 at 2:58



[1] 200


200


cat






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV