How to convert a matrix to a list of column-vectors in R?

Gavin’s answer is simple and elegant. But if there are many columns, a much faster solution would be: lapply(seq_len(ncol(x)), function(i) x[,i]) The speed difference is 6x in the example below: > x <- matrix(1:1e6, 10) > system.time( as.list(data.frame(x)) ) user system elapsed 1.24 0.00 1.22 > system.time( lapply(seq_len(ncol(x)), function(i) x[,i]) ) user system elapsed 0.2 … Read more

Flatten a list in Prolog

The definition of flatten2/2 you’ve given is busted; it actually behaves like this: ?- flatten2([a, [b,c], [[d],[],[e]]], R). R = [a, b, c] ; false. So, given the case where you’ve already bound R to [a,b,c,d,e], the failure isn’t surprising. Your definition is throwing away the tail of lists (ListTail) in the 3rd clause – … Read more

pyspark collect_set or collect_list with groupby

You need to use agg. Example: from pyspark import SparkContext from pyspark.sql import HiveContext from pyspark.sql import functions as F sc = SparkContext(“local”) sqlContext = HiveContext(sc) df = sqlContext.createDataFrame([ (“a”, None, None), (“a”, “code1”, None), (“a”, “code2”, “name2”), ], [“id”, “code”, “name”]) df.show() +—+—–+—–+ | id| code| name| +—+—–+—–+ | a| null| null| | a|code1| … Read more

Iterate over elements of List and Map using JSTL tag

Mark, this is already answered in your previous topic. But OK, here it is again: Suppose ${list} points to a List<Object>, then the following <c:forEach items=”${list}” var=”item”> ${item}<br> </c:forEach> does basically the same as as following in “normal Java”: for (Object item : list) { System.out.println(item); } If you have a List<Map<K, V>> instead, then … Read more

Find powers of 2 in a list Prolog

Here’s how you can find the “powers of two” in logically-pure way! Using sicstus-prolog 4.3.5, library(reif) and library(clpz): :- use_module([library(reif), library(clpz)]). power_of_two_t(I, T) :- L #= min(I,1), M #= I /\ (I-1), call((L = 1, M = 0), T). % using (=)/3 and (‘,’)/3 of library(reif) Sample query1 using meta-predicate tfilter/3 in combination with power_of_two_t/2: … Read more

Prolog removing unique elements only

Prolog rules are read independently of each other, so you need one rule for the case where the element is unique and one where it is not. Provided the order of the elements is not relevant, you might use: ?- remUniqueVals([A,B,C], [1,1]). A = B, B = 1, dif(C, 1) ; A = C, C … Read more

Combining (cbind) vectors of different length

You can use indexing, if you index a number beyond the size of the object it returns NA. This works for any arbitrary number of rows defined with foo: nm <- list(1:8,3:8,1:5) foo <- 8 sapply(nm, ‘[‘, 1:foo) EDIT: Or in one line using the largest vector as number of rows: sapply(nm, ‘[‘, seq(max(sapply(nm,length)))) From … Read more

Prolog – count repetitions in list

This answer shows a logically pure way to do it. The following is based on clpfd. :- use_module(library(clpfd)). We define meta-predicate tcount/3 similarly to tfilter/3! :- meta_predicate tcount(2,?,?). tcount(P_2,Xs,N) :- N #>= 0, list_pred_tcount_(Xs,P_2,0,N). :- meta_predicate list_pred_tcount_(?,2,?,?). list_pred_tcount_([] , _ ,N ,N). list_pred_tcount_([X|Xs],P_2,N0,N) :- if_(call(P_2,X), (N1 is N0+1, N1 #=< N), N1 = N0), list_pred_tcount_(Xs,P_2,N1,N). … Read more