0

I am a complete beginner in R. I installed R couple of days ago to complete my course assignment. One of the instructions was that I have to change the data format from wide to long format. The data was a time series data on Remittance inflows from 1972-2021 in Bangladesh. I had the years on the rows and amount of remittance from different countries in columns. I wrote the following code to change the data format to long format:

library(reshape2)
bd_remit_long <- melt(bd_remit,
                      id.vars = c("Saudi_Arabia", "U.A.E", "U.K", "Kuwait",
                                  "U.S.A", "Libya", "Qatar", "Oman", "Singapore",
                                  "Germany", "Bahrain", "Iran", "Japan"
                                  "Malaysia", "Australia", "Italy", "South_Korea"
                                  "Hong_Kong","Other_Countries", "Total"))

the country names mentioned here are in the columns in the initial data. After running the code my R console is showing: "Error: unexpected string constant in: " "Germany", "Bahrain", "Iran", "Japan" "Malaysia"" I have went back to check my Excel file to see if there were any string values in the observations but couldn't find any. I have multiple missing values in several country observations which are denoted by "...". So to change those missing values to 'NA', I ran this code:

unique(bd_remit$Japan)
bd_remit %>%
  select(Japan,Malaysia,Australia,Italy,South_Korea,Hong_Kong)%>%
mutate(Japan = na_if (Japan, "..."))%>%
  mutate(Malaysia = na_if (Malaysia, "..."))%>%
  mutate(Australia = na_if (Australia, "..."))%>%
  mutate(Italy = na_if (Italy, "..."))%>%
  mutate(South_Korea = na_if (South_Korea, "..."))%>%
  mutate(Hong_Kong = na_if (Hong_Kong, "..."))%>%


(I have installed all the necessary packages to run these commands) But the "..." values did not change into "NA" values either. Now I am completely stuck and I don't know what to do. I really need to change the wide data into long data. Pls help me with your valuable suggestions

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Hello, welcome to SO! The code looks good already to me. All that I can spot now is that you left a whitespace after `na_if` which might introduce the malfunction. If you are using a function, the opening bracket has to come immediately after the function name. E.g.: `na_if(Japan, "...")`. – uke Jan 22 '23 at 10:28
  • Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here so people can troubleshoot your problems (rather than a table or screenshot for example). One way of doing is by using the `dput` function on the data or a subset of the data you are using, then pasting the output into your question. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Jan 22 '23 at 10:52
  • Thank you for your kind responses. I have deleted everything and reinstalled R, because I ran into further problems, I went on to create a new variable where the code ran successfully but the variable was not getting generated. I tried multiple times. Let's see if I have any luck this time. – Fardeen Kabir Jan 23 '23 at 11:11

1 Answers1

1

It looks you are missing a couple of commas when entering the string for the id.vars argument. Specifically, these are after "Japan" and "South Korea". The following code should work.

library(reshape2)
bd_remit_long <- melt(bd_remit,
                      id.vars = c("Saudi_Arabia", "U.A.E", "U.K", "Kuwait",
                                  "U.S.A", "Libya", "Qatar", "Oman", "Singapore",
                                  "Germany", "Bahrain", "Iran", "Japan",
                                  "Malaysia", "Australia", "Italy", "South_Korea",
                                  "Hong_Kong","Other_Countries", "Total"))

Depending on how your data is formatted, you could also use the tidyr package to simplify the code. Assuming that these columns are in sequential order, the code would look something like this:

library(tidyr)
pivot_longer(bd_remit, cols = Saudi_Arabia:Total)
TWiedRW
  • 11
  • 1