Questions tagged [dcast]
267 questions
1
vote
1 answer
Get factor variable name without using apostrophs
How could I refer to a variable new in a function without writing them as string or indexing before. I want to construct a function where I can replace grouping variables easily.
For example:
final_table %>% data.table::dcast(Lipids ~…

Nadiine El Nino
- 339
- 1
- 6
1
vote
1 answer
Concatenating matches in a merge with multiple matches
I have data as follows:
input_A <- data.frame(ID = c(1,2), some_var = c("bla","more bla"))
input_B <- structure(list(ID = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 2), year = c(2001, 2002, 2003, 2001, 2002, 2003, 2001, 2002,
2003, 2001, 2002,…

Tom
- 2,173
- 1
- 17
- 44
1
vote
1 answer
Reshaping a non column name dataframe in r
I have a data frame like this:
structure(list(...1 = c(NA, NA, "name_var1", "obs1_var1", "obs2_var1"
), ...2 = c(NA, NA, "name_var2", "obs1_var2", "obs2_var2"), ...3 = c(NA,
NA, "name_var3", "obs1_var3", "obs2_var3"), ...4 = c("Dimension",…

Gonzalo S
- 23
- 5
1
vote
1 answer
Casting the difference between two vectors into a specified format
I am trying to create code that automatically creates data in the format below (for more background on the colspan see this link).
I need to do two things:
Calculate the "difference" or "distance" for lack of a better word between two…

Tom
- 2,173
- 1
- 17
- 44
1
vote
2 answers
creating matrix from three column data frame in R
I have a data frame with three columns where each row is unique:
df1
# state val_1 season
# 1 NY 3 winter
# 2 NY 10 spring
# 3 NY 24 summer
# 4 BOS 14 winter
# 5 BOS 26 spring
# 6 BOS 19 summer
# 7 WASH 99…

Munrock
- 403
- 1
- 11
1
vote
1 answer
Unconventional data frame reshaping using dcast
I want to convert table dt:
Who T Res
Pam 2 B
Pam 3 E
Pam 5 F
Bob 2 B
Bob 5 C
into
Who T1 Res1 T2 Res2 T3 Res3
Pam 2 B 3 E 5 F
Bob 2 B 5 C NA NA
using dcast or other function/s from data.table.
I have tried things such as …

pendermath
- 180
- 7
1
vote
1 answer
Using dcast to reshape a data frame
I am having trouble understanding how I can use dcast (or any other function) to restructure my data frame.
I was given a data frame that looks something like…

Adriana
- 91
- 8
1
vote
2 answers
How to create multiple columns from one column, maybe using dcast or tidyverse
I am learning R and attempting to figure out splitting a column. I am looking to spread my data from a single column in wide format. I was told to use dcast, but i haven't figured out the best way and was going to try to pipe it through tidyverse.
#…

lziegs
- 37
- 5
1
vote
1 answer
Dcast Issue: keep multiple matching rows when idvars are not unique
I have perused the existing posts on dcast and the official dcast vignette. I may not have framed my searches correctly, but I haven't found an example quite like mine. Take a look at my toy data on excel (which I can read into R). It's already in…

dunkindonts
- 11
- 5
1
vote
2 answers
R manipulating data by a row
Consider following data frame:
R
df1<-
data.frame(
ostan=rep( paste("ostan",1:3),each=12),
year=rep(c(2020,2021),each=6,len=36),
month=rep(c(1:3),each=2,len=36),
ENF=rep(letters[1:2],len=36),
Fo=1:36,
JA=36:1
,KH=c(1:12,12:1,21:32)
)
The variables…

Masoud
- 535
- 3
- 19
1
vote
2 answers
Reshaping multiple long columns into wide column format in R
My sample dataset has multiple columns that I want to convert into wide format. I have tried using the dcast function, but I get error. Below is my sample dataset:
df2 = data.frame(emp_id = c(rep(1,2), rep(2,4),rep(3,3)),
Name =…

hk2
- 487
- 3
- 15
1
vote
2 answers
cast data in wide format while retaining the alignment of variables
I have created the following dataframe in R
df<-data.frame("ID"= c("A", "A", "A", "A", "B", "B", "B"))
df$X_F= c(1,5,6, 7, 1, 7, 8)
df$X_A= c(1,5,6, 7, 1, 7, 8)
The above dataframe is in long format
ID X_F X_A
1 A 1 1
2 A 5 5
3 A 6 …

Raghavan vmvs
- 1,213
- 1
- 10
- 29
1
vote
1 answer
Reshape to wide function in R but keep some row combinations
I am trying to reshape my data to wide, trying to prep the data frame to a within-subject analysis. It is currently suitable for a between-subject analysis.
The most helpful answer I found so far was: Using the reshape function in R with multiple…

Mary Bay
- 11
- 2
1
vote
1 answer
How to restructure data with one observation by row into data with one observation by ID (and multiple columns) in R?
Let's say I have a dataframe with 3 ID columns and one column of interest. Each row represents one observation. Some ID have multiple observations, i.e., multiple rows.
df <- data.frame(id1 = c( 1, 2, 3, 4, 4),
id2 = c(…

Antonin
- 1,748
- 7
- 19
- 24
1
vote
1 answer
Is there a function within dcast that allows me to include additional conditions?
I'm trying to create a wide format dataset that would include only some of the long format data. This is data from learners going through an online learning module in which they sometimes get "stuck" in a screen, therefore have multiple attempts…

Paula de Barba
- 13
- 2