Mohamed Fawzy
Mohamed Fawzy

Reputation: 59

How to translate a "for loop in R" to Julia

How can I translate the following code of R to Julia? I am new in Julia.

I know Julia has different ways to replace for loop.

max_trial <- max(dataframe[,1])


max_c1 <- NA
  for(c in 1:max_trial){
    c1 <- which(dataframe[,1]==c)
     max_measure <- max(dataframe[c1,2])
     max_c1[c] <- max_measure
  }

As suggested I applied the following code translate


max_c1= []
for c in 1:max_trial
    c1 = findall(dataframe[:,1] .== c)
    max_c1[c] = maximum(dataframe[c1,2])
end

But I received the following error

ERROR: BoundsError: attempt to access 0-element Array{Any,1} at index [1]

Also the values received from this translation “ maximum(dataframe[c1,2])” is still different than The R code. It seems for this part of error some adjustment of the syntax needs improvement.

Upvotes: 1

Views: 132

Answers (1)

Benoit Pasquier
Benoit Pasquier

Reputation: 3005

I think the corresponding Julia code would look like

for c in 1:max_trial
    c1 = findall(dataframe[:,1] .== c)
    max_c1[c] = maximum(dataframe[c1,2])
end

although I think you did not give enough information to completely answer your question, so I'm not really sure. Maybe adding the data you used and the output you are looking for in your question would help?

Upvotes: 1

Related Questions