Questions tagged [reshape2]

The reshape2 package in R contains functions that allow data to be transformed into more convenient forms.

0
votes
0answers
20 views

Separate years in a column into multiple columns [duplicate]

I have a dataframe that looks like this: > df <- as.data.frame(cbind(c("a","a","a","a","b","b"),c("b","b","c","c","c","c"),rep(c("2005","2006"),3),c(1:6))) V1 V2 V3 V4 1 a b 2005 1 2 a ...
0
votes
2answers
23 views

How to melt and plot multiple datasets over different ranges on the same set of axis?

This is my first time posting here, I hope my question is clear and appropriate. I have a set of data the head of which looks like this: wl ex421 wl ex309 wl ex284 wl ex347 1 ...
0
votes
2answers
41 views

melt dataframe and group by using dplyr calculations in R

My sample data ` structure(list(state = c("AP", "AP"), district = c("krishna", "guntur"), rate = c(170104.5156, 1343.78134), growth_in_2016 = c(0.3844595, 0.3678), growth_in_2017 = c(0.444595, 0....
1
vote
1answer
34 views

Specify column order when casting long to wide

I want to cast a table to wide format using reshape2. I want the columns to be ordered on the vars ie as follows: data <- as.data.frame(matrix(c(rep(1,3),rep(2,3),rep(1:3,2),rep(0:2,4)),6,4)) ...
0
votes
0answers
20 views

How to convert wide format to long format data? [duplicate]

I have a dataframe (df) and I want to convert it to long format data: head(df) Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Year stn X1 30 40 153.2 316.2 526.6 410 ...
0
votes
1answer
31 views

R long to wide format categorial variables and dates

I want to make a dataset with long format with a categorial variabel (medication) that has a start and end date to wide format. As the result there should be one line per ID and for each medication a ...
0
votes
4answers
65 views

reshape2: dcast when there are multiple values for one cell but keep this values [duplicate]

I have a data frame like that: HLA_Status variable value 1 PP CCL24 9.645 2 PP CCL24 56.32 3 PP CCL24 7.268 4 PC ...
1
vote
2answers
59 views

How to match linked steps indicated by two columns of a data.table

I have a data like this: structure(list(step_origin = c(4897L, 3105L, 129L, 2689L, 2945L, 161L), step_destination = c(3105L, 1057L, 2689L, 2945L, 3201L, 673L)), row.names = c(NA, -6L), class = c("...
0
votes
1answer
39 views

How can I tidy a very messy long format data set using tidyverse or base-R functions?

I have a messy and confusing long-format data set (I have started using R very recently and could not master it yet so I need some guidance). My participants went through different phases in an ...
1
vote
1answer
19 views

Recast in R gives different value

I have the following dataframe in R DF2<-data.frame("ID"=c("A", "A", "A", "B", "B", "B", "B", 'B'), 'Freq'=c(1,2,3,1,2,3,4,5), "Val"=c(1,2,4, 2,3,4,5,8)) The datframe has the following ...
1
vote
2answers
32 views

Reshape and combine table

I have one dataframe DATA_SET, which is composed of three columnsc(TEST1, TEST2, TEST3) and a second dataframe DATA_SET1 which is composed of two columns c(key, value). DATA_SET<-data.frame( ...
0
votes
0answers
24 views

R - Tall to wide data with dplyr (or, another method) [duplicate]

Say, I have the following data: set.seed(10) dat <- data_frame( SKU = rep(c("sku1", "sku2", "sku3"), each=4), invoicedate = rep(c("12/01/2019", "12/02/2019", "12/03/2019", "12/04/2019"), 3), ...
1
vote
1answer
60 views

Need to split a column containing varying numbers of doubly concatenated data of variable names and observations

I have a column "sample_values" with varying numbers of doubly concatenated data delimited with both "," and ":" characters. I need to make the values separated by "," into new variables (columns) and ...
0
votes
1answer
53 views

how to specify ID variables in dcast?

Following is a sample of the data I have datahave # A tibble: 6 x 6 YEAR SCHOOL_NAME CONTENT_AREA BELOW_BASIC_PCT BASIC_PCT ADVANCED_PCT <dbl> <chr> ...
0
votes
1answer
23 views

r transform dataset from long to wide with all combinations

Assuming this is my dataset. Type Drat Qsec Wt Mazda 3.9 16.46 2.620 Toyota 3.85 17.02 2.875 Ford 3.15 3.44 17.02 Duster 3.21 15.84 18.3 I am interested in tranforming this ...
2
votes
2answers
46 views

How to convert long format to wide format data over multiple variable (Column) and be stacked onto each other?

I have a monthly time series data (1987-2017) for 20 station. I want to convert the long format data to wide format data such that all the data covering 20 station are in one data frame. head(...
2
votes
2answers
47 views

Creating wide data that has only 1 ID column [duplicate]

I have a data frame that looks like this: ID Code_Type Code date 1 10 4 1 1 9 5 2 2 10 6 3 2 9 7 4 and I would like it to look like ...
0
votes
1answer
30 views

long to wide - converting one column (with multiple measures) into a pair of columns

I have been trying to reshape a clustered-structure long dataset to wide format: within each cluster, the wide format will have two pairings of columns representing the long format measure. Here's ...
1
vote
2answers
60 views

Reshape dataframe so that each entry of column in repeated over all other columns

I have a data.frame, dat, which looks like this dat = data.frame(x = c(1, 1.1, 1.2, 1.3), y = c(2, 2.1, 2.2, 2.3), output = c(2, 10, 101, 100)) x y output 1 1.0 2.0 2 2 1.1 2.1 10 3 1....
0
votes
1answer
24 views

Reshape N 2-d matrices (T*M) into a 3d tensor of (T*N*M) with tidyverse/R

For each of my N variables, I have a (T * M) feature matrix, i.e., M observations per t \in T. The problem is how to convert this into a (T * N * M) array. For example, in the following example N=2, ...
1
vote
0answers
25 views

using dcast (reshape2) to mold data to wide format populates cells with count instead of value

I have a dataframe in long format (https://docs.google.com/spreadsheets/d/15jDW1pCYs7VD6MAH9GHmYrP-zf8WjsGj9F3qVXGijDM/edit?usp=sharing) that looks like this: objectid timestamp ...
0
votes
1answer
38 views

I have a long data set. How do I convert to wide format while using a rank function to limit the number of columns created?

I have a data set in a long format. Some records only have one manufacturer, some records are duplicated because there are multiple manufacturers associated with each record. For example, I have the ...
0
votes
4answers
406 views

Sequentially rename duplicate value in character variable before reshaping it with dcast

I'm scrapping cars information from a website and I'm getting not constant and not so clean data from it. I'm trying to clean and arrange this data into a data frame. For example: dd <- data....
0
votes
1answer
25 views

How to gather every 4 columns, but variables may have different length

I have a data frame that is organized with 4 columns that repeats 145. The length of the columns are not the same. I would like reshape the data frame so that I have just 4 columns. I also have to ...
1
vote
1answer
56 views

How to copy attributes from one data frame to another or to re-assign attributes to a freshly transposed data frame - R

After transposing data I'd like to re-assign attributes that are dropped. This could also be applicable to copying attributes from one data frame to another. Or copying attributes after mutates, etc., ...
2
votes
3answers
54 views

Reshaping the HRS data from wide to long format and creating a time variable

I have the following dataset (containing around 25 more variables other than Weight = W and Height = H), all spanning 10 years. Currently it has the following form and no time index. df <- ...
1
vote
2answers
61 views

Issues with plotting network in igraph

I am having some issues in realizing a bipartite network in R with the library igraph. Here is my script: library(igraph) library(reshape2) setwd("....") getwd() library(readxl) network=read_excel("...
0
votes
0answers
43 views

Stacked bart chart 4 variables with ggplot

I am very new to this and I wanted to add that the various ways in which I tried to reshape/melt the data. My data in three different variations: Version 1: year,type,total,action,perc 2015,v,"...
2
votes
1answer
35 views

Why does melt (reshape2) substitute column names by column order numbers?

I have a 74x74 pairwise distance matrix of SNP differences in which the first column and row correspond to the isolate's number, like this: 26482RR 25638 26230 25689RR 25954 26482RR 0 ...
1
vote
2answers
50 views

Plotting all observations but colour them based on different group

I have a sample data given below with sample IDs which are unique, and 3 groups. I need to plot all the observations (rows) in 'df' but color them according to the group IDs ('groupid'). Here's what I ...
0
votes
1answer
57 views

R: Gather/Spread/Reshape 21 Columns Based on 21 Other Column s

I would like to create columns based on values in some fields, populated by values in other fields. For example column1_time has value "1030" and column1_status has value "booked". I would like to ...
0
votes
1answer
39 views

draw stack chart in r

I want to draw a stack chart in R : My data set is as follows called df: df <- structure(list(id = c("A","B"), x1 = c(10,30), x2 = c(20,40), ...
0
votes
0answers
37 views

Reshape dataframe in R + generate column name

my dataframe looks like this df <- read.table(text=" id nr date 1 124 1 2017 2 122 1 2017 3 ...
-1
votes
1answer
46 views

R how to use fun.aggregate in dcast to keep discrete values when widening dataframe?

I've been trying to use the dcast() function in reshape2 to widen a large dataframe in R. However, I am not sure what to use for the aggregation function, fun.aggregate that dcast requires because I ...
0
votes
1answer
20 views

Preserve row and column order of data frame when converting data frame to matrix

I have a dataframe: df<- data.frame(lep= c("Tarchon", "Tarchon", "Tarchon", "Tarchon", "Tarchon", "Gonodonta","Gonodonta","Gonodonta", "Apatelodes"), plant=c("...
0
votes
2answers
51 views

converting pd.Series of strings into ndarray

I extract an array of words from pandas column: X = np.array(tab1['word']) example of X : array(['dog', 'cat'], dtype=object) X is a pandas Series of 665 strings. And then I transform each word ...
0
votes
1answer
108 views

how to create new variables from one variable using two rules

I would appreciate any help to create new variables from one variable. Specifically, I need help to simultaneously create one row per each ID and various columns of E, where each of the new columns ...
-1
votes
2answers
58 views

Short format to long format in R

This is what my data looks like on excel, where status and events are variables for the mentioned dates. Events columns don't have data. 30/12/2018 31/12/2018 01/01/2019 Status event ...
0
votes
1answer
169 views

How to “unmelt” data with multiple variables? [duplicate]

I am trying to clean this dataset and the original data is formatted terribly. This is the format that the original data came in: PATIENT_CODE PATIENT_NAME TEST TEST_RESULT 1 ...
2
votes
3answers
47 views

Rearrange dataframe using dcast using a dummy

I've like to use dcast function (reshape2 package) for returns a reshape data frame, but doesn't work. In my example: #Data set X<-c(804519.4,804519.6,804519.6,804519.4,804519.4,804519.4,804519.6,...
0
votes
1answer
35 views

Wrong usage of R package reshape2 to plot with ggplot2?

I would like to plot some data with ggplot2. My data is originally in wide format, and so I would like to transform it to long format first. Somehow "melt" produces a table I cannot use with ggplot2 ...
0
votes
1answer
27 views

Problem with rearrange dataframe using melt/cast

I've like to use cast function (reshape package) for returns to original dataframe state, but doesn't work. In my example: #First simulate some data set.seed(123) bands=5 data <- data.frame(matrix(...
0
votes
1answer
43 views

Is there a better way to calculate differences between columns and reorder columns after using dcast?

Below is a sample of my data. I'm trying to create data for a data table where the data must be in a very specific order after I have used the dcast function. I'm also trying to calculate the ...
1
vote
1answer
68 views

R: Casting a character column without aggregation

I want to transpose this data frame from Cat Group Rank 229 A 1 229 C 2 230 B 1 230 E 2 231 F 1 231 D 2 .. .. .. and make it look like Cat 1 ...
1
vote
0answers
59 views

R - melt and ggplot2 how to aggregate

I'm trying to understand how melt in the package reshape2 can help me plot my dataset. Currenly my dataset looks like this: df <- data.frame(factor1 = c("a","a","c","d","e"), factor2 = c("a1","a1",...
2
votes
2answers
67 views

Merge two dataframes but mean columns that exists in both

Say I have two data.frames, df = data.frame(gene = c("KRAS", "FOS"), A6 = c(20, 50), A7 = c(90, 80)) df2 = data.frame(gene = c("KRAS", "FOS"), A6 = c(20, 250) ) Here A6 is repeated and when I try ...
1
vote
3answers
85 views

R wide to long reshape with column names

I have data in this format A1 A2 B1 B2 C1 C2 10 5 11 5 21 10 And I want to convert it to: 1 2 A 10 5 B 11 5 C 21 10 How can I do it in R?
-2
votes
2answers
50 views

complex data restructuring - wide to long (multiple columns)

I want to do a restructuring as above, and I'm struggling with just using gather() as there are multiple columns to make... does anyone know a way to do this?
0
votes
0answers
47 views

Reshape2 dcast with multiple columns in R

I am trying to use the dcast function in R on a dataframe but I see something is weird in my output. x <- dcast(DT,COUNTRY + DESCRIPTION ~ DATE + MACRO_ECONOMIC_FACTOR_VALUE) Output- Instead of ...
-2
votes
1answer
41 views

Removing unwanted Observation, and calculating Time Difference

I have a data frame, when an id is audited it can be either Passed or failed at that particular time. how to calculate, the sum of all the time taken to change status from Failed to Passed state w.r.t ...