I have 2 columns in data frame , please refer to below
no value
1 A_0.9
1 B_0.8
1 C_0.7
1 D_0.7
2 B_0.9
2 D_0.8
2 A_0.7
2 C_0.7
I want to create new data frame as below
no value1 value2 value3 value4
1 A_0.9 B_0.8 C_0.7 D_0.7
2 B_0.9 D_0.8 A_0.7 C_0.7
ie: for each unique value in column "no" there will be multiple columns created using data in column "value"
t(unstack(df, value ~ no))
# [,1] [,2] [,3] [,4]
#X1 "A_0.9" "B_0.8" "C_0.7" "D_0.7"
#X2 "B_0.9" "D_0.8" "A_0.7" "C_0.7"
To tidy the above output to fit your data,
library(dplyr)
df1 <- as.data.frame(t(unstack(df, value ~ no)))
names(df1)[-1] <- paste0('value', 2:ncol(df1)-1)
rownames(df1) <- NULL
df1 <- add_rownames(df1, 'no') #from dplyr package
# no value1 value2 value3 value4
# (chr) (fctr) (fctr) (fctr) (fctr)
#1 1 A_0.9 B_0.8 C_0.7 D_0.7
#2 2 B_0.9 D_0.8 A_0.7 C_0.7
Using data.table
, we can create a sequence per unique value
by no
with rleid()
, and consequently use it to dcast()
the data to wide format.
library(data.table)
dcast(setDT(df)[, nr := rleid(value),by = no], no ~ nr)
# no 1 2 3 4
#1 1 A_0.9 B_0.8 C_0.7 D_0.7
#2 2 B_0.9 D_0.8 A_0.7 C_0.7
Or with the dev version (1.9.7) of data.table
, the following is possible, thanks @Arun!
dcast(setDT(df), no ~ rowid(no, prefix = 'value'))
# no value1 value2 value3 value4
#1: 1 A_0.9 B_0.8 C_0.7 D_0.7
#2: 2 B_0.9 D_0.8 A_0.7 C_0.7
I would use reshape
library, which wraps a nice set of data manipulation functions. Example to accomplish your task:
n = c(1,1,1,1,2,2,2,2)
x = c('A', 'B', 'C', 'D', 'A', 'B', 'C', 'D')
# Just to create the column names you showed in the example
columns = rep(paste("value", 1:4, sep=""), 2)
data = data.frame(n, columns, x)
cast(data, n~columns)
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.