-1

I want to convert table A into table B. How can I do in Python and R. Are there any specific libraries?

Kindly check the attached image

enter image description here

camille
  • 16,432
  • 18
  • 38
  • 60
Xaaz
  • 3
  • 3
  • In `R`, you can do `df1 %>% pivot_longer(cols =R3:R5)` – akrun Feb 22 '20 at 17:43
  • Thank you. Btw do these tables refer as matrix/list or dataframe and how can we create in R? – Xaaz Feb 22 '20 at 17:55
  • if it is from a excel or csv, you can read with `read.csv` into a data.frame `df1 <- read.csv('yourfile.csv') and then it should work with `pivot_longer` – akrun Feb 22 '20 at 17:56
  • 1
    "Btw do these tables refer as matrix/list or dataframe" -- without your data (like actual workable data, not a screenshot of a spreadsheet), we don't know the answer to this any better than you do. It's better if you can make a [mcve] and ideally pick one of the two languages you've tagged – camille Feb 22 '20 at 18:02
  • It's not working brother – Xaaz Feb 24 '20 at 20:39

1 Answers1

0

In r, you can use pivot_longer function from tidyr package:

library(tidyr)
TABLE_B <- pivot_longer(TABLE_A, everything(), names_to = "ITEM Names", values_to = "ALL")
dc37
  • 15,840
  • 4
  • 15
  • 32
  • Thank you. Btw do these tables refer as matrix/list or dataframe and how can we create in R? – Xaaz Feb 22 '20 at 17:55
  • They should be data.frames. If you are working on Excel, you can export TableA as csv and read it in R using`read.table` or `read.csv` for example. There is a lot of tutorials online that show you how to import dataset in R. – dc37 Feb 22 '20 at 17:57
  • It's still not working. I didn't specify the table names such as Table_A and Table_B? I am getting this error Error: could not find function "pivot_longer" – Xaaz Feb 24 '20 at 20:39
  • you need to install `tidyr` and load the library. – dc37 Feb 24 '20 at 20:42
  • I have installed the library – Xaaz Feb 24 '20 at 21:41
  • So, you just need to load it using `library(tidyr)` and the command `pivot_longer` should work. – dc37 Feb 24 '20 at 21:42
  • Is there anyway to work with dlpyr library? if yes, how can I do that? – Xaaz Feb 24 '20 at 21:49
  • `tidyr` is part of `tidyverse` library as `dplyr`. https://www.tidyverse.org/ – dc37 Feb 24 '20 at 21:53
  • My data is changed now I am getting this error > TABLE_B <- pivot_longer(TABLE_A, everything(), names_to = "UPC", values_to = "Product") Error in unique(names(data)) : object 'TABLE_A' not found I didn't mention anywhere the Table names – Xaaz Feb 24 '20 at 21:54
  • Where is your dataframe called TABLE_A ? You need to have in your r environment a dataframe called "TABLE_A" to be able to process it. – dc37 Feb 24 '20 at 22:00
  • Well my real data is similar like that except table names and In the first table there are only 3 columns and some NA values and the new table that I need to create should be 2 columns that will reflect all 3 columns values into 2 columns fields along with removing Na's – Xaaz Feb 24 '20 at 22:05
  • So, you need to replace "TABLE_A" by the name of your current table. To remove `NA`, you can do after having perform the `pivot_longer` command : `TABLE_B <- TABLE_B[!is.na(TABLE_B$ALL),]` – dc37 Feb 24 '20 at 22:08
  • I am so sorry to bother you again and again. I don't have any table names. I just have headers in the data tables – Xaaz Feb 24 '20 at 22:33
  • How do you load your table in R ? Are you using Rstudio ? – dc37 Feb 24 '20 at 22:34
  • Yes I am using R studio cloud – Xaaz Mar 12 '20 at 15:54
  • Do you see your tables into the environment section ? How to do you load your tables ? – dc37 Mar 12 '20 at 15:56