r - Converting column values to row and column names -



r - Converting column values to row and column names -

i have dataset 2 columns, x$x0 , x$x1 , below values in dataset x, there more 1234876 observations in datasets because of many duplicate values.

x0 x1 ---------------- 0 1 0 2 1 0 1 3 2 1 2 3 . . . . . . 1234876 1230000

i want create matrix using unique values in column1 (x$x0) , unique values in column2 (x$x1). values in x$x0 row names , values in x$x1 column names.

then assign value 1 cells relation exits between x$x0 , x$x1 , final results should this.....

| 0 1 2 3 .......1230000 -------------------------------- 0 | 1 1 | 1 | 1 1 | 2 | 1 1 | 3 | | . | | . | | . | | 1234876 | | --------------------------------

hope makes sense :(, advise on how helpful.

it's little hard tell asking, work? should create info frame x0 values rows , x1 values columns. observations become nas set other things in there.

edit: i've updated based on changes , using dput output. creates matrix row names correspond x0 , colnames correspond x1.

df <- structure(list(x0 = c(0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 4l), x1 = c(2l, 3l, 4l, 5l, 0l, 2l, 4l, 5l, 15l, 0l, 11l, 12l, 13l, 14l, 63l, 64l, 65l, 66l, 67l, 7l)), .names = c("x0", "x1"), row.names = c(na, 20l), class = "data.frame") library('reshape2') df_new <- dcast(df, x0 ~ x1, function(x) ifelse(length(x) >= 1, 1, 0)) rownames(df_new) <- df_new$x0 as.matrix(df_new[-1]) # 0 2 3 4 5 7 11 12 13 14 15 63 64 65 66 67 # 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 # 1 1 1 0 1 1 0 0 0 0 0 1 0 0 0 0 0 # 2 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 # 3 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 # 4 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0

r

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -