reshape

how to pivot/unpivot (cast/melt) data frame? [duplicate]

只谈情不闲聊 提交于 2019-12-28 02:06:28
问题 This question already has answers here : Reshaping data.frame from wide to long format (7 answers) Closed 3 years ago . How can I 'unpivot' a table? What is the proper technical term for this? UPDATE: The term is called melt I have a data frame for countries and data for each year Country 2001 2002 2003 Nigeria 1 2 3 UK 2 NA 1 And I want to have something like Country Year Value Nigeria 2001 1 Nigeria 2002 2 Nigeria 2003 3 UK 2001 2 UK 2002 NA UK 2003 1 回答1: I still can't believe I beat

R Reshape data frame from long to wide format?

走远了吗. 提交于 2019-12-27 10:34:27
问题 What's the best way to convert the data frame below from long to wide format? I tried to use reshape but didn't get the desired results. 2015 PROD A test1 2015 PROD A blue 2015 PROD A 50 2015 PROD A 66 2015 PROD A 66 2018 PROD B test2 2018 PROD B yellow 2018 PROD B 70 2018 PROD B 88.8 2018 PROD B 88.8 2018 PROD A test3 2018 PROD A red 2018 PROD A 55 2018 PROD A 88 2018 PROD A 90 回答1: A possible solution is this library(tidyverse) df = read.table(text = " year prod value 2015 PRODA test1 2015

Converting vectors into 2D matrix [duplicate]

谁都会走 提交于 2019-12-25 16:51:50
问题 This question already has answers here : Reshape three column data frame to matrix (“long” to “wide” format) [duplicate] (6 answers) Closed 6 years ago . I have data that looks like this: total position division 34 C ATL 34 C CEN 47 C NE 46 C NW 44 C PAC 42 C SE 57 D ATL 50 D CEN 44 D NE 52 D NW 42 D PAC 52 D SE 29 L ATL 34 L CEN 28 L NE 34 L NW 29 L PAC 24 L SE 26 R ATL 33 R CEN 25 R NE 29 R NW 24 R PAC 35 R SE I wish to transform it into a 2D matrix which can then be used for a Chi squared

Converting vectors into 2D matrix [duplicate]

我的梦境 提交于 2019-12-25 16:51:12
问题 This question already has answers here : Reshape three column data frame to matrix (“long” to “wide” format) [duplicate] (6 answers) Closed 6 years ago . I have data that looks like this: total position division 34 C ATL 34 C CEN 47 C NE 46 C NW 44 C PAC 42 C SE 57 D ATL 50 D CEN 44 D NE 52 D NW 42 D PAC 52 D SE 29 L ATL 34 L CEN 28 L NE 34 L NW 29 L PAC 24 L SE 26 R ATL 33 R CEN 25 R NE 29 R NW 24 R PAC 35 R SE I wish to transform it into a 2D matrix which can then be used for a Chi squared

Problems converting from wide to long using reshape - tibble issue? [duplicate]

限于喜欢 提交于 2019-12-25 14:45:22
问题 This question already has answers here : Reshaping data.frame from wide to long format (7 answers) Closed 2 years ago . I am trying to convert my data from wide to long. I have an id variable and 131 columns of time data. All columns are integer format. The data was imported in csv format. > class(ECGHR) [1] "tbl_df" "tbl" "data.frame" Here are the first 6 rows of data for 22 time-varying variables: > head (ECGHR) # A tibble: 6 × 132 id T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15

Turning table data into columns and counting by frequency

放肆的年华 提交于 2019-12-25 09:14:31
问题 I have a dataframe in the following form: shape is 2326 x 1271 Column names are just serialized from 0-1269 while rows are categories that could repeat like "apple" in the example. The internal data points can represent anything (let's say they represent stores in this example) and I'm trying to convert them into columns and having the data points become the number of times that category shows up in that "store". Visually, here is the table I'm trying to get to: Note that Apple shows up in AA

Why does this array-reshaping routine work outside of a function but not inside of a function?

自闭症网瘾萝莉.ら 提交于 2019-12-25 08:59:32
问题 I am trying to convert a list into a numpy array with a specified number of columns. I can get the code to work outside the function as follows: import numpy as np ls = np.linspace(1,100,100) # Data Sample ls = np.array(ls) # list --> array # resize | outside function ls.resize(ls.shape[0]//2,2) print(ls) >> [[ 1. 2.] [ 3. 4.] . . . [ 97. 98.] [ 99. 100.]] I do not understand my error when trying to throw the routine in a function. My attempt is as follows: # resize | inside function def

Using Reshape Cast [duplicate]

痞子三分冷 提交于 2019-12-25 08:16:09
问题 This question already has answers here : How to reshape data from long to wide format (11 answers) Closed 2 years ago . I am trying to reshape an example dataframe by the following. df<-data.frame(market = c("a","b","c","a","b","c"),companyName = c("foo","foo","foo", "bar","bar","bar"), val = seq(1,6)) require(reshape) dfNew <- cast(df,market ~ companyName+companyName) To generate: market company 1 company 2 1 a 1 4 2 b 2 5 3 c 3 6 But I get this error: Using val as value column. Use the

R: Reshape from long to wide, order of reshaped columns as original column order [duplicate]

旧时模样 提交于 2019-12-25 06:13:03
问题 This question already has answers here : Convert data from long format to wide format with multiple measure columns (5 answers) Closed last year . Below is an excerpt from my data: data <- as.data.frame(matrix(c(rep(1,3),rep(2,3),rep(1:3,2),rep(0:2,4)),6,4)) colnames(data) <- c("id", "rater", "x", "y") print(data) # id rater x y # 1 1 1 0 0 # 2 1 2 1 1 # 3 1 3 2 2 # 4 2 1 0 0 # 5 2 2 1 1 # 6 2 3 2 2 I reshaped it from long to wide: reshape(data, timevar = "rater", idvar = "id", direction =

tensorflow: can reshape() create a copy?

北慕城南 提交于 2019-12-25 03:18:28
问题 In v4 of their API, torch has introduced reshape() , to align more with the style of numpy. Previously, changing the shape of a torch tensor was done with view() . I wondered whether view() was going to be deprecated now and looked at the docs. Turns out that reshape() is not just a numpy-friendly alias for view() , actually, it has a different semantic. Torch tries to give you contiguous memory where possible. If the new view dimensions violate a contiguity constraint, you have to explicitly