Questions tagged [reshape2]
The reshape2 package in R contains functions that allow data to be transformed into more convenient forms.
1,160
questions
0
votes
1
answer
38
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
30
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
45
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
30
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
11
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
28
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
9
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
30
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
29
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
49
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
52
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
62
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
71
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
36
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
47
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
25
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 ...
1
vote
0
answers
85
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
38
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
73
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
35
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
88
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
31
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
74
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
439
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
36
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
368
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
11
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
25
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
26
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 ...
1
vote
1
answer
54
views
r arrange data nested wide format
I have a dataset like this
Time1 Time2 Time3
A
Median 0.046 0.12 0
Q1, Q3 -0.12, 0.22 -1.67, -4.59 -0.245,...
2
votes
1
answer
101
views
Stack bars with percentages and values shown
Here is my dataframe - data_long1
data.frame(
value = c(88, 22, 100, 12, 55, 17, 10, 2, 2),
Subtype = as.factor(c("lung","prostate",
"...
0
votes
2
answers
155
views
why sklearn LinearRegression model does not accept one dimensional data?
I'm trying to learn the basics of Linear Regression.
I tried to build the simplest model with the simplest data for the starter .
for data I had:
## Data (Apple stock prices)
apple = np.array([155, ...
2
votes
2
answers
82
views
reshape from wide to long, simple data.table issue
I know there must be a one-line data.table solution for this, probably with dcast, but I can't figure it out.
I have data like this:
library(data.table)
data1 <- data.table(
id = seq(1:5),
code ...
0
votes
1
answer
381
views
Plotting column graph with multiple groups using ggplot
I have dataset below and would like to have the results in the picture attached using ggplot.
bar_df
class boys girls
a 20 30
b 40 30
c 50 50
...
0
votes
3
answers
87
views
How to do a nested 2 level reshape in R?
I am currently working on a survey project that asks about respondents' economic activities. So we will ask the jobs/businesses they have, and since some jobs/businesses are seasonal, we ask about ...
0
votes
0
answers
14
views
how to arrange a table by putting a column variable in several row? [duplicate]
I am trying to arrange a table with two columns :
V1 V2
1 F3_Ct10_1.sub Ct10
2 F3_Ct10_2.sub Ct10
3 F3_Ct10_3.sub Ct10
4 F3_Ct11_1.sub Ct11
5 ...
1
vote
1
answer
27
views
Expand dataset by count column in Dplyr
I have a dataset as follows:
library(tidyverse)
df <- data.frame(
report_date = c("2020-03-14", "2020-03-14", "2020-03-19", "2020-03-20"),
...
0
votes
2
answers
67
views
Changing dataframes in bulk? How to apply a list of operations to multiple dataframes?
So, I have 6 data frames, all look like this (with different values):
Now I want to create a new column in all the data frames for the country. Then I want to convert it into a long df. This is how I ...
2
votes
1
answer
58
views
Spread a dataframe and relabel column names
Probably a tidyr or a reshape2 question.
I have this type of a data.frame:
df <- data.frame(group = c("A","B","C","D"),
id1 = c("AV14D&...
5
votes
3
answers
391
views
Pivoting to a longer format using pivot_longer
I'm trying to pivot to a longer format using dplyr::pivot_longer, but can't seem to get it to do what I want. I can manage with reshape::melt, but I'd also like to be able to achieve the same using ...
1
vote
2
answers
57
views
Converting rows into columns based on the values in the rows, in R
This is the table I am working with. I want to convert reservation into separate columns.
I want it to be transformed into something like this. I have been trying to do this using reshape2 and dplyr'...
1
vote
0
answers
41
views
Reshaping multiple columns with disordered values
I have a dataset with multiple columns that I want to reshape. Essentially, the data from the columns corresponding to the numerals become columns taking their corrsponding value in that row from the ...
0
votes
1
answer
445
views
Convert columns into rows, R data.frame [duplicate]
I want to repeat heach column into a new row as the next example:
Date<- c(seq(as.Date("2000/1/1"), by = "month", length.out = 3))
A<- c(seq(2,4,length.out=3))
B<- c(seq(...
0
votes
1
answer
329
views
Data manipulation using dcast R
I have a data as shown below:
Data
Flag
Answer
X
1
Yes
X
2
Yes
Y
1
Yes
Y
2
No
Z
1
Yes
Z
2
Yes
and I want to convert it to:
Data
1
2
X
Yes
Yes
Y
Yes
No
Z
Yes
Yes
I know I have to use dcast ...
-2
votes
1
answer
50
views
How best to use R to reshape dataframe from long to wide and combine values
I have a dataframe of about 2000 rows and 3 columns. In essence, I want to reshape this dataframe to be wider than longer. This is an example of my current data:
ID
Procedure
Date
D55
Sedation
01/01/...
3
votes
6
answers
456
views
Split a string by two delimiters only in the first occurrence
I have read many examples here and other forums, tried things myself, but still can´t do what I want:
I have a string like this:
myString <- c("ENSG00000185561.10|TLCD2", "...
3
votes
2
answers
53
views
How to recode values in a column if the value equals the column name, across an entire dataset in R
I have a very large dataset that I'm trying to wrangle. Here's a few of the first few rows of the variables I'm interested in for the purposes of this question:
id
stressor
1
Homelessness
1
...
0
votes
1
answer
135
views
Spread or dcast and fill in counts [duplicate]
Probably a basic question.
I have a key - value data.frame(df below):
features <- paste0("f",1:5)
set.seed(1)
ids <- paste0("id",1:10)
df <- do.call(rbind,lapply(ids,...
2
votes
1
answer
167
views
R- How to reshape Long to Wide with multiple variables/columns
I started off with the following subset of my data
UserID labelnospaces responses
1 Were you given any info? yes
1 By using this service..? yes
1 How satisfied are you? ...