Hi all. I have two questions, one very basic and one not so basic.
I have a DataFrame that looks like this:
df = DataFrame() df.list = [[1,2,3,6,12,13,14],[20,21,23,24,50],[1,3,5,7,10,11,12,13]]
And I need to generate a new column, that only stores the “points” of each array. This means, only the first and last number in a sequence of consecutive numbers.
It would look like this:
df.new = [[1,3,12,14],[20,21,23,24],[10,13]] #it is important that they are made up of pairs!
My solution, so far, looks something like this:
df.list2 = [,,] for r in eachrow(df) #loop the df j=0 #this is for "walking" down the array for i in r.list j+=1 if j == 1 #append the fist number. This needs to go, maybe the fist numbers doesn't have a consecutive pair. append!(r.list2, r[j]) elseif j != 1 if (r[j]-1) != r[j-1] #check if the number is not consecutive to the previous one append!(r.list2, r[j]) elseif j == length(r) #This also should go. It works to append the last value, but it might be an isolated one! append!(r.list2,r[j]) end end end end df
That kinda works. With the first and last part of the loop, I can append the “points” of each string, and with the middle I can identify some of the numbers that I need. Here is the output:
df.list2 = [[1,6,12,14],[20,23,50], [1,3,5,7,10,13]]
The problem is, first, I’m not sure how to create an empty column automatically, so I can append all my values (if I do something like
df.new = missings(Array, nrow(df)), that is what I usually do, I get an error, because I am trying to append values, as it were a list).
Secondly, I could use some help with the loop. I thought about using
diff, so I could save me some trouble, but I am not sure if it will improve the loop.
Right now I am still getting the “lone” numbers (i e
1,3,5,7 in the last list), and I don’t really know how to get rid of them, and I should find a way to append the last number in the list (in the second list, I can append the
20, 23, but I can’t append the
21, 24, or the
3, 12 on the first one).
I think I can do it with a little more wresting with the for loop, and the different conditions, but any help is welcome.
Thanks a lot! cheers