作为arules算法的数据准备的一部分,我有以下格式的数据。
public class GraphicalBoard extends JComponent {
private int xSize = 50;
private int ySize = 50;
private Board board;
private int size;
public GraphicalBoard() {
}
public void setBoard(Board board){
this.board = board;
}
public void paintComponent(Graphics g) {
super.paintComponent();
if(board == null) {
throw new RuntimeException("Board not set") ;
}
String colors[] = {"BLUE","GREEN","YELLOW","RED","BLACK","ORANGE","PINK"};
Graphics2D g2 = (Graphics2D) g;
int xCoord = 0;
int yCoord = 0;
int colorNum = 0;
int size = board.getSize() ;
String colorOfSquare = "";
for (int count = 0; count < size; count++) {
for (int counter = 0; counter < size; counter++) {
colorNum = board.getBoard(count, counter) ;
colorOfSquare = colors[colorNum];
g2.setColor(colorOfSquare);
Rectangle square = new Rectangle(xCoord,yCoord,xSize,ySize);
xCoord += 50;
}
yCoord += 50;
}
}
我需要交易文件
userid p1 p2 p3
1 1 0 0
2 1 1 0
3 0 1 1
4 0 1 0
你能帮我解决这个问题。
感谢。
答案 0 :(得分:0)
一种选择是在逻辑which
上使用matrix
并提取row.names
row.names(which(t(df1[-1])==1, arr.ind = TRUE))
#[1] "p1" "p1" "p2" "p2" "p3" "p2"
答案 1 :(得分:0)
您需要做的就是将数据转换为logical
。
library("arules")
dat <- data.frame(
userid = 1:4,
p1 = c(1,1,0,0),
p2 = c(0,1,1,1),
p3 = c(0,0,1,0)
)
dat2 <- dat[,-1]
for(i in 1:ncol(dat2)) dat2[[i]] <- as.logical(dat2[[i]])
dat2
p1 p2 p3
1 TRUE FALSE FALSE
2 TRUE TRUE FALSE
3 FALSE TRUE TRUE
4 FALSE TRUE FALSE
tran <- as(dat2, "transactions")
inspect(tran)
items transactionID
[1] {p1} 1
[2] {p1,p2} 2
[3] {p2,p3} 3
[4] {p2} 4