简体   繁体   中英

Convert a vector to data.frame, one column for each unique value

We are given a vector, like this:

x <- c(1,2,1,5,2,1,2,5,1)

What we need is a data.frame say y having number of rows equal to length(x) and number of columns equal to length(unique(x)) , that means one column per unique item in x , such that y[i,j]==TRUE if and only if the i th element of x is the j th unique item of x (assigned to column j ):

y <- data.frame("1"=x==1, "2"=x==2, "5"=x==5, check.names=F)

A simple way to perform this is:

y <- setNames(data.frame(sapply(unique(x), function(i) x==i)), unique(x))

Do you have a better idea (ie a particular function)?

How about using outer ?

outer( x , unique(x) , `==` )
#      [,1]  [,2]  [,3]
# [1,]  TRUE FALSE FALSE
# [2,] FALSE  TRUE FALSE
# [3,]  TRUE FALSE FALSE
# [4,] FALSE FALSE  TRUE
# [5,] FALSE  TRUE FALSE
# [6,]  TRUE FALSE FALSE
# [7,] FALSE  TRUE FALSE
# [8,] FALSE FALSE  TRUE
# [9,]  TRUE FALSE FALSE

Obviously finishing it all off would be wrapping it like so...

setNames( data.frame( outer( x , unique(x) , `==` ) ) , unique( x ) )

If you can live with a binary representation instead of a logical representation of your data, I would just use table :

y <- table(seq_along(x), x)

To get a data.frame , use as.data.frame.matrix :

as.data.frame.matrix(y)
#   1 2 5
# 1 1 0 0
# 2 0 1 0
# 3 1 0 0
# 4 0 0 1
# 5 0 1 0
# 6 1 0 0
# 7 0 1 0
# 8 0 0 1
# 9 1 0 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM