Home > Mobile >  Ungroup column R and assign binary variable for each unique entry
Ungroup column R and assign binary variable for each unique entry

Time:10-21

I have following dataframe [df] in R:

Flair Text Time
YOLO asder 10:01
Fluff qetrtgf 10:02
Fluff eqargrstgfh 10:04
Fluff qrettzuh 10:05
Fluff erghgfxbhs 10:17
Art and Media qaerzgh wtruws 10:27
Charity eztzutzui 10:31
Memes etzuj 10:54
Question rehbgfd 10:55
Provocative hetzjas 10:56
... ... ...

which i like to transform to this:

Text Time Flair_YOLO Flair_Fluff Flair_Art ...
asder 10:01 1 0 0 ...
qetrtgf 10:02 0 1 0 ...
eqargrstgfh 10:04 0 1 0 ...
qrettzuh 10:05 0 1 0 ...
erghgfxbhs 10:17 0 1 0 ...
qaerzgh wtruws 10:27 0 0 1 ...
eztzutzui 10:31
etzuj 10:54
rehbgfd 10:55
hetzjas 10:56
... ...

As the problem is hard to describe i was looking into ungroup() but was unable to find the correct formula.

CodePudding user response:

This is a reshaping problem from long to wide format. But, since you are asking some additional arrangements I did not consider this as a duplicate.

Firstly, we can create an additional column with ones for 'value' requirement in reshaping by,

df$Flair_ <- 1

Than by using the reshape function from BaseR we can get the result.

out <- reshape(df, idvar = c("Text","Time"), 
        timevar ="Flair",sep = "",
        direction = "wide")
out[is.na(out)] <- 0 # changing NA columns to 0
out

gives,

#             Text  Time Flair_YOLO Flair_Fluff Flair_Art and Media ...
#1           asder 10:01          1           0                   0
#2         qetrtgf 10:02          0           1                   0
#3     eqargrstgfh 10:04          0           1                   0
#4        qrettzuh 10:05          0           1                   0
#5      erghgfxbhs 10:17          0           1                   0
#6  qaerzgh wtruws 10:27          0           0                   1

Data:

df <- read.table(text="
Flair,Text,Time
YOLO,asder,10:01
Fluff,qetrtgf,10:02
Fluff,eqargrstgfh,10:04
Fluff,qrettzuh,10:05
Fluff,erghgfxbhs,10:17
Art and Media,qaerzgh wtruws,10:27
Charity,eztzutzui,10:31
Memes,etzuj,10:54
Question,rehbgfd,10:55
Provocative,hetzjas,10:56",sep=",",header=T,stringsAsFactors=F)
  • Related