reshape

R - Concatenate cell in dataframe, by group, depending on another cell value

两盒软妹~` 提交于 2021-02-08 10:36:14
问题 I have a dataset of the following type (first row is the header): content is always text merge is always a logical id1 id2 start_line end_line content merge A B 1 1 "aaaa" TRUE A B 4 4 "aa mm" TRUE A B 5 5 "boool" TRUE A B 6 6 "omw" TRUE C D 6 6 "hear!" TRUE C D 7 7 " me out!" TRUE C D 21 21 "hello" FALSE Problem: I need to merge following a very specific criteria: Rows that have merge = FALSE must remain as is Rows that have: same id1 , same id2 and consecutive start_line : Need to be

reshaping a data frame in pandas

安稳与你 提交于 2021-02-08 08:16:19
问题 Is there a simple way in pandas to reshape the following data frame: df = pd.DataFrame({'n':[1,1,2,2,1,1,2,2], 'l':['a','b','a','b','a','b','a','b'], 'v':[12,43,55,19,23,52,61,39], 'g':[0,0,0,0,1,1,1,1] }) to this format?: g a1 b1 a2 b2 0 12 43 55 19 1 23 52 61 39 回答1: In [75]: df['ln'] = df['l'] + df['n'].astype(str) In [76]: df.set_index(['g', 'ln'])['v'].unstack('ln') Out[76]: ln a1 a2 b1 b2 g 0 12 55 43 19 1 23 61 52 39 [2 rows x 4 columns] If you need that ordering then: In [77]: df.set

How to transform a dataframes row into columns in R?

送分小仙女□ 提交于 2021-02-08 06:43:30
问题 I have a data frame which I need to transform. I need to change the rows into unique columns based on the value of a column. ex: The Input DataFrame | column_1 | column_2 | ----------------------- | A | B | | A | C | | B | E | | B | C | | C | F | | C | G | The Output DataFrame | column_1 | column_2 | column_3 | ---------------------------------- | A | B | C | | B | E | C | | C | F | G | The final DataFrame should have all the unique values in column_1 and the values from column_2 from input

Split image tensor into small patches

谁说胖子不能爱 提交于 2021-02-07 07:12:26
问题 I have an image of shape (466,394,1) which I want to split into 7x7 patches. image = tf.placeholder(dtype=tf.float32, shape=[1, 466, 394, 1]) Using image_patches = tf.extract_image_patches(image, [1, 7, 7, 1], [1, 7, 7, 1], [1, 1, 1, 1], 'VALID') # shape (1, 66, 56, 49) image_patches_reshaped = tf.reshape(image_patches, [-1, 7, 7, 1]) # shape (3696, 7, 7, 1) unfortunately does not work in practice as image_patches_reshaped mixes up the pixel order (if you view images_patches_reshaped you will

Split image tensor into small patches

若如初见. 提交于 2021-02-07 07:11:53
问题 I have an image of shape (466,394,1) which I want to split into 7x7 patches. image = tf.placeholder(dtype=tf.float32, shape=[1, 466, 394, 1]) Using image_patches = tf.extract_image_patches(image, [1, 7, 7, 1], [1, 7, 7, 1], [1, 1, 1, 1], 'VALID') # shape (1, 66, 56, 49) image_patches_reshaped = tf.reshape(image_patches, [-1, 7, 7, 1]) # shape (3696, 7, 7, 1) unfortunately does not work in practice as image_patches_reshaped mixes up the pixel order (if you view images_patches_reshaped you will

Pivot Table-like Output in R?

折月煮酒 提交于 2021-02-05 17:23:34
问题 I am writing a report that requires the generation of a number of pivot tables in Excel. I would like to think there is a way to do this in R so that I can avoid Excel. I would like output like the screenshot below (teacher names redacted). As far as I can tell, I could use the reshape package to calculate the aggregate values, but I'd need to do that a number of times and somehow get all of the data in the correct order. At that point, I should just be doing it in Excel. Does anyone have any

Pivot Table-like Output in R?

删除回忆录丶 提交于 2021-02-05 17:22:34
问题 I am writing a report that requires the generation of a number of pivot tables in Excel. I would like to think there is a way to do this in R so that I can avoid Excel. I would like output like the screenshot below (teacher names redacted). As far as I can tell, I could use the reshape package to calculate the aggregate values, but I'd need to do that a number of times and somehow get all of the data in the correct order. At that point, I should just be doing it in Excel. Does anyone have any

Pivot Table-like Output in R?

巧了我就是萌 提交于 2021-02-05 17:21:40
问题 I am writing a report that requires the generation of a number of pivot tables in Excel. I would like to think there is a way to do this in R so that I can avoid Excel. I would like output like the screenshot below (teacher names redacted). As far as I can tell, I could use the reshape package to calculate the aggregate values, but I'd need to do that a number of times and somehow get all of the data in the correct order. At that point, I should just be doing it in Excel. Does anyone have any

Pivot Table-like Output in R?

為{幸葍}努か 提交于 2021-02-05 17:20:41
问题 I am writing a report that requires the generation of a number of pivot tables in Excel. I would like to think there is a way to do this in R so that I can avoid Excel. I would like output like the screenshot below (teacher names redacted). As far as I can tell, I could use the reshape package to calculate the aggregate values, but I'd need to do that a number of times and somehow get all of the data in the correct order. At that point, I should just be doing it in Excel. Does anyone have any

wide to long and col name into row name

拥有回忆 提交于 2021-02-05 12:29:52
问题 I have a table looks like this: v1 <- c("A","B","C") v2 <- c("E","G","") v3 <- c("B","C","D") df1 <- data.frame(v1,v2,v3) and I want to change it to What should I do. Please give me multiple ways if it is possible. Thanks. 回答1: You can get the data in long format and then remove the blank values. library(dplyr) library(tidyr) gather(df1, variable, value) %>% filter(value != '') # variable value #1 v1 A #2 v1 B #3 v1 C #4 v2 E #5 v2 G #6 v3 B #7 v3 C #8 v3 D However, gather has been retired