Creating an R dataframe row-by-row

You can grow them row by row by appending or using rbind(). That does not mean you should. Dynamically growing structures is one of the least efficient ways to code in R. If you can, allocate your entire data.frame up front: N <- 1e4 # total number of rows to preallocate–possibly an overestimate DF <- … Read more

Splitting a list of integers into a list of positive integers and a list of negative integers

The recursive part is not quite correct. split([], [], []). split([Head|Tail], [Head|List1], List2) :- Head>=0, split(Tail, List1, List2). split([Head|Tail], List1, [Head|List2]) :- Head<0, split(Tail, List1, List2). The Head should be added to the positive list if Head >= 0 and to the negative list when Head < 0. Moreover, checking the sign of Head at … Read more

What is the most efficient way to cast a list as a data frame?

I think you want: > do.call(rbind, lapply(my.list, data.frame, stringsAsFactors=FALSE)) global_stdev_ppb range tok global_freq_ppb 1 24267673 0.03114799 hello 211592.6 2 11561448 0.08870838 world 1002043.0 > str(do.call(rbind, lapply(my.list, data.frame, stringsAsFactors=FALSE))) ‘data.frame’: 2 obs. of 4 variables: $ global_stdev_ppb: num 24267673 11561448 $ range : num 0.0311 0.0887 $ tok : chr “hello” “world” $ global_freq_ppb : num … Read more

Segregating Lists in Prolog

A logically pure implementation is very straight-forward, thanks to clpfd: :- use_module(library(clpfd)). list_evens_odds([],[],[]). list_evens_odds([X|Xs],[X|Es],Os) :- X mod 2 #= 0, list_evens_odds(Xs,Es,Os). list_evens_odds([X|Xs],Es,[X|Os]) :- X mod 2 #= 1, list_evens_odds(Xs,Es,Os). Some sample queries we expect to succeed (with a finite sequence of answers): ?- Xs = [1,2,3,4,5,6,7], list_evens_odds(Xs,Es,Os). Xs = [1,2,3,4,5,6,7], Es = [ 2, 4, … Read more