reshape

Reshape data frame from wide to long with re-occuring column names in R

。_饼干妹妹 提交于 2019-12-22 09:31:50
问题 I'm trying to convert a data frame from wide to long format using the melt formula. The challenge is that I have multiple column names that are labeled the same. When I use the melt function, it drops the values from the repeat column. I have read similar questions and it was advised to use the reshape function, however I was not able to get it work. To reproduce my starting data frame: conversion.id<-c("1", "2", "3") interaction.num<-c("1","1","1") interaction.num2<-c("2","2","2") conversion

Reshape a data frame from long to wide

百般思念 提交于 2019-12-22 08:20:43
问题 I have some issues to change the shape of my df. Data: id <- c(1,2,3,4,1,4,1,2,3) a <- c("A","B","C","D","A","D","A","B","C") b <- c(1,1,1,1,2,2,3,3,3) c <- c(12,10,12,23,16,17,7,9,7) df <- data.frame(id,a,b,c) which results in: id a b c 1 A 1 12 2 B 1 10 3 C 1 12 4 D 1 23 1 A 2 16 4 D 2 17 1 A 3 7 2 B 3 9 3 C 3 7 I would like to get the following structure where column b corresponds to month: id a 1 2 3 1 A 12 16 7 2 B 10 NA 9 3 C 12 NA 7 4 D 23 17 NA I tried: df2 <- reshape(df, timevar = "b

Transpose duplicated rows to column in R

隐身守侯 提交于 2019-12-22 06:59:42
问题 I have a large data.frame (20000+ entries) in this format: id D1 D2 1 0.40 0.21 1 0.00 0.00 1 0.53 0.20 2 0.17 0.17 2 0.25 0.25 2 0.55 0.43 Where each id may be duplicated 3-20 times. I would like to merge the duplicated rows into new columns, so my new data.frame looks like: id D1 D2 D3 D4 D5 D6 1 0.40 0.21 0.00 0.00 0.53 0.20 2 0.17 0.17 0.25 0.25 0.55 0.43 I've manipulated data.frames before with plyr, but I'm not sure how to approach this problem. Any help would be appreciated.Thanks. 回答1

r reshape data long to wide with unknown number of columns

左心房为你撑大大i 提交于 2019-12-22 06:49:56
问题 I'm sure this is trivial but I can't find how to do it. I have a data frame in which there are individuals, each of which can have several properties, and each property is classified in a number of ways. Currenly it's in long shape, with a record looking like (in schematic form, actually it's a little more complicated): IndividualID Property PropClass 1 X A 1 Y B 2 X A 3 Y B 3 W C 3 Z A What I want is one row for each individual ID, with the individual ID and then pairs of columns for each

reshaping data frame in pandas

旧时模样 提交于 2019-12-21 21:35:31
问题 Let's say I have this data frame: df = pd.DataFrame({'n':[0 ,1 ,0 ,0 ,1 ,1 ,0 ,1],'l':[12 ,16 ,92, 77 ,32 ,47, 22, 14], 'cols':['col1','col1','col1','col1','col2','col2','col2','col2']}) and this is what I'm trying to get: col1 col2 l n l n 12 0 32 1 16 1 47 1 92 0 22 0 77 0 14 1 I've been playing around with set_index and stack / unstack methods but with no success... 回答1: import pandas as pd df = pd.DataFrame( {'n':[0 ,1 ,0 ,0 ,1 ,1 ,0 ,1],'l':[12 ,16 ,92, 77 ,32 ,47, 22, 14], 'cols':['col1

Using reshape in Python to reshape an array

耗尽温柔 提交于 2019-12-21 20:04:02
问题 I have an array that looks like below: array([[0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7, 7, 7]]) How can I use reshape to divide it into 4 chucks, such that it looks like array([[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6], [7

Converting a dataframe from “wide” format to “long” format in R

雨燕双飞 提交于 2019-12-21 17:32:06
问题 I have the following dataframe: df = data.frame(A_1 = c(1,2,3), A_2 = c(4,5,6), A_3 = c(7,8,9), B_1 = c(10, 11, 12), B_2 = c(13, 14, 15), B_3 = c(16, 17, 18)) #> df # A_1 A_2 A_3 B_1 B_2 B_3 #1 1 4 7 10 13 16 #2 2 5 8 11 14 17 #3 3 6 9 12 15 18 The column names contain both a letter and a number. The letter refers to a specific variable (e.g A is a factor, B is a factor), while the numbers in the column names, refer to individuals. In other words, each individual has values for A and B: A_1

Create table with all pairs of values from one column in R, counting unique values [duplicate]

两盒软妹~` 提交于 2019-12-21 12:35:40
问题 This question already has an answer here : Table of Interactions - Case with pets and houses (1 answer) Closed 4 years ago . I have data that shows what customers have purchased certain items. They can purchase an item multiple times. What I need is a table that shows all of the possible pairwise combinations of items along with the unique number of customers who have purchased that combination (the diagonal of the table will just be the unique number of people purchasing each item). Here is

numpy中reshape的用法

自闭症网瘾萝莉.ら 提交于 2019-12-21 12:24:08
numpy . reshape ( a , newshape , order = 'C' ) [ source ] a:数组–需要处理的数据 newshape:新的形狀–整数或整数数组,如(2,3)表示2行3列,新的形状应该与原来的形状兼容,即行数和列数相乘后等于a中元素的数量 order: 可选范围为{‘C’, ‘F’, ‘A’}。使用索引顺序读取a的元素,按照索引顺序将元素放到变换后的数组中。默认参数为C。 1)“C”指的是用类C写的读/寫索引顺序的元素,最后一个维度变化最快,第一个维度变化最慢。以二维数组为例,就是横着读,横着写,按行讀,按行填寫新的形狀。即优先读/写一行。 2)“F”是指用FORTRAN类索引顺序读/写元素,最后一个维度变化最慢,第一个维度变化最快。竖着读,竖着写,优先读/写一列。注意,“C”和“F”选项不考虑底层数组的内存布局,只引用索引的顺序。 3)“A”是所生成的数组的效果与原数组a的数据存储方式有关,如果数据是按照FORTRAN存储的话,它的生成效果与”F“相同,否则与“C”相同。这里可能听起来有点模糊,下面会给出示例。 使用reshhape的例子 r1 = np.reshape(a,(-1,1),order='F') r1 = p.reshape((-1,1),order='F') 注意:通过reshape生成的新数组和原始数组共同使用一个内存块

Python Reshape 3d array into 2d

和自甴很熟 提交于 2019-12-21 09:16:09
问题 I want to reshape the numpy array as it is depicted, from 3D to 2D. Unfortunately, the order is not correct. A assume to have a numpy array (1024, 64, 100) and want to convert it to (1024*100, 64). Does anybody has an idea how to maintain the order? I have a sample data data[0,0,0]=1 data[0,1,0]=2 data[0,2,0]=3 data[0,3,0]=4 data[1,0,0]=5 data[1,1,0]=6 data[1,2,0]=7 data[1,3,0]=8 data[2,0,0]=9 data[2,1,0]=10 data[2,2,0]=11 data[2,3,0]=12 data[0,0,1]=20 data[0,1,1]=21 data[0,2,1]=22 data[0,3,1