reshape

Is there an R function to reshape this data from long to wide?

断了今生、忘了曾经 提交于 2021-01-30 02:38:32
问题 How the data looks now: Coach ID | Student | score | --------------------------------- 1 | A | 8 | 1 | B | 3 | 2 | A | 5 | 2 | B | 4 | 2 | C | 7 | To look like this: Coach ID | Student | score | student_2|score_2| student_3|score_3 ------------------------------------------------------------------ 1 | A | 8 | B | 3 | 2 | A | 5 | B | 4 | C | 7 Is there anyway to reshape data from long to wide? Thanks! 回答1: You could create a new identifier column with unique value for every student and then

Given that i have a 2d array and i want to reshape it to 1d with one value per row

烈酒焚心 提交于 2021-01-29 18:15:09
问题 This is my array arr = np.array([[0, 1], [3, 4], [6, 7]]) flat_arr = np.reshape(arr, -1) am getting the following result: [0 1 2 3 4 5 6 7 8] my desired result is : [0] [1] [3] [4] [5]... 回答1: There are several ways to do it: flat_arr[:, None] flat_arr[:, np.newaxis] np.expand_dims(flat_arr, axis=1) Additionally, you could just reshape it like so: arr.reshape(-1, 1) 回答2: You can use this new shape: import numpy as np arr = np.array([[0, 1], [3, 4], [6, 7]]) flat_arr = np.reshape(arr, (arr

which.min within reshape2's dcast()?

萝らか妹 提交于 2021-01-29 08:31:47
问题 I would like to extract the value of var2 that corresponds to the minimum value of var1 in each building-month combination. Here's my (fake) data set: head(mydata) # building month var1 var2 #1 A 1 -26.96333 376.9633 #2 A 1 165.38759 317.3993 #3 A 1 47.46345 271.0137 #4 A 2 73.47784 294.8171 #5 A 2 107.80130 371.7668 #6 A 2 10.16384 308.7975 Reproducible code: ## create fake data set: set.seed(142) mydata1 = data.frame(building = rep(LETTERS[1:5],6),month = sort(rep(1:6,5)),var1=rnorm(30,50

How to convert array into special items of structured array and revert it back?

 ̄綄美尐妖づ 提交于 2021-01-29 07:11:50
问题 I want to perform some numpy methods on items of structured array instead of numbers. So, for example, while working with array of integers of shape (4, 3), I need to convert it to array of items of length 3 and perform some operations as it were a single one dimensional array of shape (4,). These conversions itself, unfortunately, looks really complicated for me. Let's take another example: n, m, r = 2, 3, 4 array = np.arange(n*m).reshape((n,m)) dt = np.dtype(','.join('i'*m)) arr1 = np.array

R dates as column names containing duplicate values (need to retain original date)

时光毁灭记忆、已成空白 提交于 2021-01-29 05:31:56
问题 I have a dataset I'm trying to tidy up. I read in the file with read.xlsx, contained in the header is date values that I need to retain their values even when duplicated when I gather/spread the data. The data set looks like the below. The dates from excel read in as numbers (which is fine) the issue is that there can be duplicate dates (e.g. 43693) , which I need to keep their original values. Date 43693 43686 43686 43714 43693 1 Contract 111 222 333 444 555 2 Org1 NR NB NR NB P 3 Org2 P P P

How to reshape an array while preserving order?

时间秒杀一切 提交于 2021-01-28 20:11:52
问题 I am trying to reshape an array from shape (a,b,c) to (b,c,a) such that if my table is B and my new table is B1 then B[n,:,:]=B1[:,:,n] for some n between 0 and a. For example I tried something like, B=np.array([[[1],[2],[23]],[[2],[4],[21]],[[6],[45],[61]],[[1],[34],[231]]]) B1=np.reshape(B,(3,1,4)) But B[1,:,:]=array([[ 2],[ 4],[21]]) and B1[:,:,1]=array([[ 2],[21],[ 1]]) which is not what I want I would've expected them to be equal. Any suggestions would be greatly appreciated. 回答1: In

Reshape data from long to wide with multiple measure columns using spread() or other reshape functions

你。 提交于 2021-01-28 08:50:14
问题 I know there're already many questions here asked about this topic. But after I looked over a few, all of them have only one 'measure' column. I still couldn't figure out how to do it with my data. My data look like the following with X1, X2 indicate different regions. so each column in this data set represents all the ages collected for a single region. age X1 X2 age 0 2 2 age 1 2 2 age 2 2 3 ... I want to reshape the data to wide form: age 0 age 1 age 2 X1 2 2 2 X2 2 2 3 ... To recreate the

Pandas long to wide format with multi-index

巧了我就是萌 提交于 2021-01-27 12:31:35
问题 I have a dataframe that looks like this: data.head() Out[2]: Area Area Id Variable Name Variable Id Year \ 0 Argentina 9 Conservation agriculture area 4454 1982 1 Argentina 9 Conservation agriculture area 4454 1987 2 Argentina 9 Conservation agriculture area 4454 1992 3 Argentina 9 Conservation agriculture area 4454 1997 4 Argentina 9 Conservation agriculture area 4454 2002 Value Symbol Md 0 2.0 1 6.0 2 500.0 That I would like to pivot so that Variable Name is the columns, Area and Year are

Recurrent neural networks for Time Series with Multiple Variables - TensorFlow

北慕城南 提交于 2021-01-27 05:42:27
问题 I'm using previous demand to predict future demand, using 3 variables , but whenever I run the code my Y axis shows error If I use only one variable on the Y axis separately it has no error. Example: demandaY = bike_data[['cnt']] n_steps = 20 for time_step in range(1, n_steps+1): demandaY['cnt'+str(time_step)] = demandaY[['cnt']].shift(-time_step).values y = demandaY.iloc[:, 1:].values y = np.reshape(y, (y.shape[0], n_steps, 1)) DATASET SCRIPT features = ['cnt','temp','hum'] demanda = bike

How can I generate by-group summary statistics if my grouping variable is a factor?

老子叫甜甜 提交于 2021-01-27 05:16:27
问题 Suppose I wanted to get some summary statistics on the dataset mtcars (part of base R version 2.12.1). Below, I group the cars according to the number of engine cylinders they have and take the per-group means of the remaining variables in mtcars . > str(mtcars) 'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: