Is there a way to transform a three column data frame into two columns while using the same data in R?

coding_guy0042

I have a data frame as follows:

id    day    count
1     mon    3
1     tues   4
1     wed    8
2     mon    6
2     tues   3

and I would like to transform it to look something like this:

id   mon  tues  wed 
1    3    4     8
2    6    3     NA

Any ideas? Thanks!

G. Grothendieck

Using the data shown reproducibly in the Note at the end here are a few ways. No packages are used.

# 1
xtabs(count ~., DF)
##    day
## id  mon tues wed
##  1   3    4   8
##  2   6    3   0

# 2
tapply(DF[[3]], DF[-3], c, default = 0)
##    day
## id  mon tues wed
##  1   3    4   8
##  2   6    3   0

# 3
r <- reshape(DF, dir = "wide", timevar = "day")
r[is.na(r)] <- 0
r
##   id count.mon count.tues count.wed
## 1  1         3          4         8
## 4  2         6          3         0

Note

Lines <- "id    day    count
1     mon    3
1     tues   4
1     wed    8
2     mon    6
2     tues   3"
DF <- read.table(text = Lines, header = TRUE)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how do i transform a column of data frame while keeping the remaining data frame same in Pyspark?

Transform data frame rows into columns using duplicate ID in R

Is there a way to plot two columns of a data frame in the same axis?

Is using OPENXML() the best way to transform data in an XML Column to Rows and Columns?

how to transform many columns of a data frame in r

R - Removing the same name in two columns of a data frame

R - Match column by other columns in same data frame

in R how to apply a value of a column to multiple columns in the same data frame

replace each value of three columns of a data frame with the value of a column in a second data frame in R

Spliting string in column by seperator and adding those as new columns in the same data frame using R

Transform two arrays in to one data frame in R

R transform data frame columns into single column based on column name split

Merging every three columns of a data frame in R

How do I transform a 1 column data frame into a vector while the column name stays the vector name in R

R: convert nested JSON in a data frame column to addtional columns in the same data frame

rbinding two data frame with same num of columns

Stacking two data frame columns into a single separate data frame column in R

Combining two columns in a data frame and creating a new column in an existing data frame in R

How to compare two columns of one data.frame with one specific column of another data.frame in R?

Reshape the data from three columns to two column

Transform same column row data with multiple columns data to new dataframe

R - Using reshape() to turn data frame into a two column matrix

transform data.frame matrix-column into columns

How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration?

Combine same header columns in R data frame

R How to expand data frame column matrices into data frame columns

Transformation of a vector to a data frame with two columns in R

Searching Two Columns in a Data Frame in R

How to merge two columns in a data frame in R?