All Questions

143
votes
8answers
80661 views

How to reshape data from long to wide format?

I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c("firstName", "secondName"), each=4), numbers = rep(1:4, 2), value = rnorm(8) ...
75
votes
5answers
45579 views

Reshaping data.frame from wide to long format

I have some trouble to convert my data.frame from a wide table to a long table. At the moment it looks like this: Code Country 1950 1951 1952 1953 1954 AFG Afghanistan 20,249 ...
92
votes
2answers
42951 views

Reshape three column data frame to matrix ("long" to "wide" format)

I have a data.frame that looks like this. x a 1 x b 2 x c 3 y a 3 y b 3 y c 2 I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like: ...
18
votes
5answers
2751 views

Reshaping multiple sets of measurement columns (wide format) into single columns (long format)

I have a dataframe in a wide format, with repeated measurements taken within different date ranges. In my example there are three different periods, all with their corresponding values. E.g. the first...
11
votes
6answers
2446 views

Transpose / reshape dataframe without "timevar" from long to wide format

I have a data frame that follows the below long Pattern: Name MedName Name1 atenolol 25mg Name1 aspirin 81mg Name1 sildenafil 100mg Name2 atenolol 50mg Name2 enalapr...
59
votes
5answers
38131 views

Gather multiple sets of columns

I have data from an online survey where respondents go through a loop of questions 1-3 times. The survey software (Qualtrics) records this data in multiple columns—that is, Q3.2 in the survey will h...
48
votes
6answers
26484 views

Split delimited strings in a column and insert as new rows

I have a data frame as follow: +-----+-------+ | V1 | V2 | +-----+-------+ | 1 | a,b,c | | 2 | a,c | | 3 | b,d | | 4 | e,f | | . | . | +-----+-------+ Each of the alphabet i...
6
votes
2answers
10673 views

Frequency counts in R

This may seem like a very basic R question, but I'd appreciate an answer. I have a data frame in the form of: col1 col2 a g a h a g b i b g b h c i I want to transform it into coun...
6
votes
1answers
1481 views

dcast restructuring from long to wide format not working

My df looks like this: Id Task Type Freq 3 1 A 2 3 1 B 3 3 2 A 3 3 2 B 0 4 1 A 3 4 1 B 3 4 2 A 1 4 ...
9
votes
3answers
17038 views

Cast multiple value columns

I have the following data frame and i want to use cast to create a "pivot table" with columns for two values (value and percent). Here is the data frame: expensesByMonth = structure(list(month = c("...

Previous Next