Questions tagged [reshape2]
The reshape2 package in R contains functions that allow data to be transformed into more convenient forms.
1,176
questions
0
votes
1
answer
48
views
Wide to long format dataframe, where the wide dataframe has paired columns (AAA1 with value11, BBB1 with value22, CCC1 with value33 etc)
I am trying to convert a wide dataframe that contains the following columns:
id, age, gender, AAA1, value11, AAA2, value12, ..., BBB1, value21, BBB2, value22, ..., CCC1, value31, CCC2, value32,...
...
1
vote
2
answers
42
views
In R, how to transpose row data to coulmn per variable? [duplicate]
I have some data like below
dataA=data.frame(structure(list(Season = c(2021, 2021, 2021, 2021, 2021, 2022,
2022, 2022, 2022, 2022, 2023, 2023, 2023, 2023, 2023),
...
0
votes
0
answers
32
views
Reshape multiple columns R. Columns inverted
Following this question I got a very strange behaviour with base reshape:
Reshaping wide to long with multiple values columns
Here, it works:
df <- data.frame(id=c("a", "b", &...
2
votes
3
answers
68
views
Convert the event-level dataset to the patient-level data in r
I need to convert the event-level dataset to the patient-level data i.e. convert a long dataset to a wider one based on deidnum as a key variable. Additionally, I want to create columns for each ...
0
votes
1
answer
17
views
I would like to reshape my data such that the individual items under the column ''variable'' become separate columns with their values under them [duplicate]
The current data looks like this: Please run the code below
df1<- data.frame(Names= c(c(rep("Lincon",12), rep("Joe",12),rep("Megi",12))),
Year= c(rep(...
0
votes
0
answers
23
views
Issue using data.frame() and/or cast() to alter data in R
I am working with a large dataset that has many different test parameters and their results. This is for water quality, so each row of data is for a concentration of phosphorus, nitrogen, dissolved, ...
0
votes
0
answers
60
views
Error in sum(X) : invalid 'type' (list) of argument
I am in need of help with this problem.
I have been using this script for about 2 years with no issues. However, I recently had to format my PC and therefore reinstall R and RStudio.
The code is the ...
0
votes
1
answer
34
views
Reshaping data into long forma
I have two large data frames, the sample below shows what they look like:
Name<-c("ST01","ST02","ST03")
B1<-c(0.01,0.003,0.04)
B2<-c(0.02,0.002,0.02)
B3<-c(0....
1
vote
0
answers
36
views
"Aggregation requires fun.aggregate: length used as default" error when using cast() function from Reshape package
I am trying to use the cast() function from the Reshape package to convert a long format dataframe into wide format. Below is the dataframe (named textMessagesLong) I am working with.
I am using the ...
0
votes
0
answers
38
views
Error when using unnest() following pivot_wider() in R
I have a dataset that I am trying to reshape as wider
The original dataset is something like this
#df
Cell_line Gene AF
COLO320 MYC 0.5
COLO320 KRAS 0.4
H524 ...
2
votes
2
answers
82
views
Reframing output of confidence intervals to combine mean, upper and lower values into one cell
I use the code below to calculate the mean, upper and lower confidence intervals of multiple variables at once.
library(gmodels)
library(purrr)
dfci <- df %>%
group_by(group) %>%
dplyr::...
2
votes
2
answers
94
views
How to make four combination of points with the same ID in a data frame in R?
I am currently trying to make squares (polygons) from a data frame in R and in order to do that (according this guide), I need to have a data frame with 4 sets of paired points as their lon-lat ...
0
votes
1
answer
33
views
I want to modify my dataset in R so that I can conduct a market basket analysis
I need to modify in R my dataset that looks like a classic table with observations in the row and variables in the column. Specifically, the first column reports a set of transactions identified by an ...
0
votes
1
answer
30
views
How to handle "variable" and "value" in melt reshape2 with Roxygen2
I'm trying a different method to boxplot two population means.I'm using reshape2's melt function which comes inbuilt with 2 columns, variable and value. Below is the code. It works but I get a warning ...
1
vote
2
answers
61
views
dcasting long table along multiple columns
I am having a difficult time understanding dcast, and cannot get the right commands to get what I want. I will give a minimal repro.
#generate the data
ID <- c('a','a','a','b','b','b')
Parameter &...
4
votes
1
answer
50
views
transform data frame from wide to long (several variables) using separator [duplicate]
I have a df like this one:
FID <- c(1,2,3,4)
d <- c(1,1,2,2)
IID_1 <- c("1A","2A","NA","4A")
IID_2 <- c("1B","2B","3B",...
-1
votes
1
answer
57
views
Reshaping/Deduping long data to wide in R [duplicate]
I've been trying to reshape this long data into wide format by ID within excel with no luck. I tried using dcast but it did not give me the results I was expecting.
I've attached a csv of how the data ...
0
votes
1
answer
51
views
How to melt a dataframe with tidyverse, and create a new column
I have pet survey data from 6 households.
The households are split into levels (a,b).
I would like to melt the dataframe by aminal name (id.var), household (var.name), abundance (value.name), whilst ...
0
votes
2
answers
44
views
reshape function not mapping values correctly
I am working with a within-subjects design looking at how participants rated 20 videos across a variety of variables (valence, arousal, etc.) and am trying to turn my wide-format df into a long format ...
0
votes
0
answers
20
views
How to collapse a wide dataframe into a long dataframe in R? [duplicate]
I currently have a dataframe that I would like to collapse that has the following columns
Name - Attempt - Outcome1 - Outcome2 - Outcome3
I would like to reformat it into the following single ...
0
votes
1
answer
50
views
Got stuck while changing data format from wide to long
I am a complete beginner in R. I installed R couple of days ago to complete my course assignment. One of the instructions was that I have to change the data format from wide to long format. The data ...
0
votes
3
answers
52
views
dcasting a dataframe using reshape2::dcast
I am trying to reshape a dataframe using dcast function of reshape2 package
library(reshape2)
dat = data.frame(aa = c('A', 'B', 'C', 'C', 'B'))
dcast(dat, aa~aa)
This generates below output
aa A B ...
1
vote
1
answer
47
views
transforming long data to wide in R
Hi i have data that looks like this :
X snp_id is_severe encoding_1 encoding_2 encoding_0 chisq p.value AF_total AF_LATIN
1 1 chr21-10139833-A-C 0 0 1 ...
0
votes
2
answers
38
views
reshape data frame into colnames and rownames
My questions is self-explained by the example, but in short: I would to dcast the data frame and add 1 and NA depending on the matched that are in the original data frame. I tried reshape2::dcast(cars~...
0
votes
0
answers
24
views
Running ANOVA test after melt function
I am trying to run an ANOVA test after using melt and the error keeps popping up. I do not know what I'm doing wrong
Error in eval(predvars, data, env) : object 'W' not found
rm(list=ls())
...
0
votes
1
answer
29
views
How to stack values in one column by factor name in another column in R
I have a data frame with 4 columns, here is the structure:
Abundance DayNight peak Family
1 2 Day Peak Pangasiidae
2 1 Night Non-Peak Cyprinidae
3 ...
1
vote
1
answer
11
views
What am I doing wrong in this dcast (reshape2) function?
So I have this table
and I need to arrange it by team as rows and year (2010 - 2015) as attributes, so Ive tried
with xtabs and table function, but I only get the count, instead of the points that ...
1
vote
1
answer
36
views
Reshape binary data group by column and count
I want to reshape a data.frame to a matrix in the following format:
Input:
Typ1 Typ2 Maths Science English History
1 1 1 1 1 1
0 1 0 1 0 ...
0
votes
1
answer
40
views
Tidy up rbind(ddply()) R for transforming from wide to long
So I have figured out a solution on how I can transform my data from wide to long. The dataset had 190 columns representing 19 variables for 10 years (and additionally 31 columns that did not require ...
1
vote
2
answers
88
views
remove missing values from dataframe and reshape the dataframe with the non-missing ones
I have a dataframe that has missing values at each column, but at different rows. For simplicity, let's see the following dataframe (real dataframe is much more complex):
first_column <- c(1, 2, NA,...
1
vote
0
answers
70
views
r - How to reshape data from long to wide format (when you have messy data, many columns, and want to retain some of the original structure)
I have a dataset that I want to convert from long to wide format. I have checked the two most popular answers (here and here), but I can't make sense of them as they apply to my data structure.
I want ...
0
votes
1
answer
45
views
transforming complex wide data to long in R, multiple year columns, corresponding values, UTF-8 issues (Duplicate?)
What I have is a wide format, that need to get reshaped and sorted differently. I have 10 columns in total for year, one same year for all rows, different year per column. These I would like to merge ...
0
votes
1
answer
112
views
Convert single row to a column in R
I have a data frame that has serveral columns along with sub-columns and it looks like this:
I want to transform this data frame and make a new column from second row which can be considered as a sub ...
2
votes
1
answer
78
views
R Pivot Longer With Multiple Columns
HAVE = data.frame( COURSE =c( 1, 1, 1, 2, 2, 2, 3, 3, 3 ),
STUDENT =c( 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C' ),
FISH =c( 4, 8, 9, 1, 7, 1, 10, 10, 10 ),
CAT =c( 9, 8, 10, 7,...
2
votes
2
answers
44
views
Reshape the data from three columns to two column
I have a data like this :
Response Prompt Prompt.Label Comments
1 Friend who si3 3_a
2 1 was si4_en 3_a
3 1 was si4_nag 3_a
4 ...
3
votes
2
answers
65
views
Melt list of identical dataframes to one dataframe in R
I have a list waterlevels of nine identical dataframes containing information about piezometers K01 to K09. Each of these dataframes contains 96860 rows, all of them having 13 variables, except for ...
0
votes
0
answers
26
views
reshape2 melt a list of pixel image objects like in reshape
I have a list of pixel image objects from spatstat.geom (see example data below) and want to melt them into one data.frame. I do it with reshape::melt and it works fine. Now, I wanted to do it with ...
7
votes
4
answers
502
views
Is there an alternative to reshape2::melt for multidimensional arrays in base R or tidyverse?
Suppose I have a 3-dimensional array g with dimensions [x,y,z]. reshape2::melt(g) would produce a data frame with columns giving indices of x,y,z and value where value contains the values in each ...
0
votes
1
answer
40
views
Wide to long omitting the early NAs
I have this simple data frame,
colx <- c(2002-01, 2002-02, 2002-03, 2002-04)
cola <- c(10, 20, NA, 40)
colb <- c(NA, NA, NA, 400)
colc <- c(1000, 2000, 3000, 4000)
df <- data.frame(...
0
votes
2
answers
88
views
Convert data.frame from long to wide with rank in R
I have a data.frame as follows:
d1 = data.frame(
g = factor(rep(1:3, rep(4,3))),
x = c(21,24,27,20,20,18,19,15,22,25,27,22)
)
d2 = data.frame(d1, r=rank(d1$x)) #add ranks of x-elements
d2
# g ...
0
votes
2
answers
44
views
Split string based on a key word in r programming, keyword= BAS, Getting duplicate results. especially FRM_id = 1014
if (!require("librarian"))install.packages("librarian")
librarian::shelf(stringr,stringi,dplyr,reshape2)
FRM_ID<-c(1006,1007,1011,1012,1014,1014,1015)
FRM_Name<-c("...
0
votes
0
answers
157
views
converting a df from wide to long but warning message: number of items to replace is not a multiple of replacement length
I would like to turn this dataframe from wide to long. I know that there are some similar posts about the topic, for instance I have tried all the options from this one:
Reshaping wide to long with ...
0
votes
0
answers
42
views
Reshape the bike share recorded data into bike share counts
Everyone
I am struggling to reshape the bike share recorded data.
data link :
https://www.kaggle.com/code/samratp/bike-share-analysis/data?select=NYC-2016-Summary.csv
The original data form is ...
4
votes
4
answers
86
views
Convert semi-long data into wide data
I'm very sure there should be a simple alternative but I'm not able to figure it out. Currently using a for loop which is not optimal.
My dataframe is like this:
NAME <- c("ABC", "...
0
votes
1
answer
740
views
R package Error: package or namespace load failed for ‘reshape2’ in loadNamespace: there is no package called ‘magrittr’
I have installed install.packages("reshape2") on R 3.4.3
but when I try to use that library like
library(reshape2)
I get the below error
Error: package or namespace load failed for ‘...
0
votes
3
answers
47
views
R -- transform tab delimited string to long format
How to you reshape a string delimited by a tab or space into a long format? The string (called label here) can be of different lengths.
I have this
var label
1 work 100 101
2 sleep 500 ...
1
vote
2
answers
666
views
Referring to the row above when using mutate() in R
I want to create a new variable in a dataframe that refers to the value of the same new variable in the row above. Here's an example of what I want to do:
A horse is in a field divided into four zones....
0
votes
0
answers
12
views
Reshape to wide format with a time column [duplicate]
This I am certain is a duplicate question. Pardon me for this violation. I have a dataset like this in a long format.
ID TIME Col1 Value
A 1 X1 25
B 1 X1 26
C 1 ...
0
votes
1
answer
34
views
R split by group and create new columns
I'm trying to split a data frame from long to wide format by converting selected rows to columns. Here is the current general long-format structure:
data_long <- data.frame(
id = c("kelp&...
1
vote
2
answers
27
views
Pivot data frame without Null
I have looked to all similar question of stackoverflow but without success.
Consider the iris data set. I just want to put the species as columns, and get below each species names, only the Sepal ...