Questions tagged [dcast]
267 questions
10
votes
3 answers
Means multiple columns by multiple groups
I am trying to find the means, not including NAs, for multiple columns withing a dataframe by multiple groups
airquality <- data.frame(City = c("CityA", "CityA","CityA",
"CityB","CityB","CityB",
…

Jen
- 203
- 1
- 2
- 12
9
votes
1 answer
Spread with duplicate identifiers (using tidyverse and %>%)
My data looks like this:
I am trying to make it look like this:
I would like to do this in tidyverse using %>%-chaining.
df <-
structure(list(id = c(2L, 2L, 4L, 5L, 5L, 5L, 5L), start_end = structure(c(2L,
1L, 2L, 2L, 1L, 2L, 1L), .Label =…

Rasmus Larsen
- 5,721
- 8
- 47
- 79
8
votes
2 answers
wide format with dcast data.table
I would like to transform a table like this (*):
set.seed(1)
mydata <- data.frame(ID=rep(1:4, each=3), R=rep(1:3, times=4), FIXED=rep(runif(4), each=3), AAA=rnorm(12), BBB=rbinom(12,12,0.5), CCC=runif(12))
ID R FIXED AAA BBB CCC
1 1 …

skan
- 7,423
- 14
- 59
- 96
6
votes
3 answers
dcast fails to cast character column when the data size is large
I'm using the dcast function in the library(reshape2) package to cast a simple table of three columns
df = data.table(id = 1:1e6,
var = c('continent','subcontinent',...),
val = c('America','Caribbean',...)````
by…

Lamothy
- 337
- 4
- 17
5
votes
4 answers
how to calculate unique count using dcast in R
I'm using dcast to transpose the following table
date event user_id
25-07-2020 Create 3455
25-07-2020 Visit 3567
25-07-2020 Visit 3567
25-07-2020 Add …

Abhi
- 55
- 3
5
votes
0 answers
How can I refer to multiple aggregation functions in data.table dcast?
I want to put data.table dcast function into a function, which can handle custom number/order of aggregate functions. That is why I need to pass aggregate functions as parameters to dcast function. The parameters need to be defined outside…

tgyozo
- 51
- 2
5
votes
2 answers
Using dcast.data.table with date values and aggregation
Trying to figure this out. Suppose you have a data.table:
dt <- data.table (person=c('bob', 'bob', 'bob'),
door=c('front door', 'front door', 'front door'),
type=c('timeIn', 'timeIn', 'timeOut'),
…

ds_practicioner
- 733
- 8
- 20
5
votes
1 answer
Widening a dataframe to get monthly sums of revenue for all unique values of catogorical columns in R
I have a df which has data like this:
sub = c("X001","X002", "X001","X003","X002","X001","X001","X003","X002","X003","X003","X002")
month = c("201506", "201507", "201506","201507","201507","201508", "201508","201507","201508","201508", "201508",…

ljourney
- 515
- 4
- 11
5
votes
1 answer
dcast error: `Error in match(x, table, nomatch = 0L)`
I have a dataframe called df that looks something like…

Username
- 3,463
- 11
- 68
- 111
4
votes
2 answers
data.table: Create new character column based on indicator columns values and names
I have a data.table with 1.6x10^8 records and I want to create a new character column based on the indicator column names for where there is a value of 1.
For example,
library(data.table)
DT <- data.table::data.table(ID=c("a","a","a","b","b"),
…

theneil
- 488
- 1
- 4
- 14
4
votes
1 answer
Split a row into columns with conditions in R
I've a dataframe as under
+----+-------+---------+
| ID | VALUE | DATE |
+----+-------+---------+
| 1 | 10 | 2019-08 |
| 2 | 12 | 2018-05 |
| 3 | 45 | 2019-03 |
| 3 | 33 | 2018-03 |
| 1 | 5 | 2018-08 |
| 2 | 98 | 2019-05…

user11845701
- 157
- 1
- 9
4
votes
3 answers
Order columns in dcast
How do I specify the column order, based on column 'Col' when using dcast?
df <- dcast(x, ID ~ ColumnName, value.var = "Answer")
I need the solution to be non specific to the data as x can be the results of any question (thus Col can be 1-3 or 1-2…

shujufenxishi
- 53
- 1
- 5
3
votes
2 answers
Efficient data wrangling with missing data in data.table
I have a data.table similar to the following:
foo bar a1 a2 a3 b1 b2 b3 b4 c1 c2 A_1 A_2 A_3 C_1 C_2
m 19 0 1 2 2 1 3 0 0 2 25 33 61 50 50
f 30 1 2 1 0 4 2 1 2 2 10 43 30 45 73
n 22 0 2 2 1 3 1 0 1 …

Jinglestar
- 376
- 1
- 10
3
votes
2 answers
Subsetting a long-data.table using values of a column within the data.table and casting the other values
I have a 22 million observation rows data table of the following form:
`dt <- data.table(
firm_id = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
metric = c("AN_BILANT", "OPEX", "CAPEX","AN_BILANT","OPEX", "CAPEX", "AN_BILANT", "OPEX", "CAPEX",…

newmathlearner_7
- 31
- 2
3
votes
4 answers
Transforming long table into wide format with counts for only one column
I have a table in long format as shown below, every row is unique in this input table :-
year variable
2014 ab
2014 cd
2014 ef
2016 ef
2016 gh
2014 ab
2014 cd
2014 ef
2016 ef
2016 gh
I would…

relu
- 333
- 1
- 3
- 18