0

I am running a long code not written by me. There is only one snag in the entire code and it is ruining the overall output. The line of code is:

>ppl_inventory_split_rates <- read.csv(file=ppl_inventory_split, header=TRUE, sep=" ")

>for (i in 1:rows) {

>if (ren_ppl[i, "year"] >= start_year && ren_ppl[i, "year"]<= end_year) {

+ y = paste('X',as.character(ren_ppl_split4[i, "year"]), sep = '', collapse= NULL) 

# The split rate from ppl_inventory_split_rates is used to split values between PP_EX_OTH and PP_NEW
rateDC_PP_EX_OTH = ppl_inventory_split_rates[ppl_inventory_split_rates["Years"]==ren_ppl_split4[i, "year"] & 
                                               ppl_inventory_split_rates["Act_abb_ppl"]=="DC","PP_EX_OTH_ppl"]

rateOS1_PP_EX_OTH = ppl_inventory_split_rates[ppl_inventory_split_rates["Years"]==ren_ppl_split4[i, "year"] & 
                                                ppl_inventory_split_rates["Act_abb_ppl"]=="OS1","PP_EX_OTH_ppl"]

The line of error says:

Error in [.data.frame(ppl_inventory_split_rates, "Years") :
undefined columns selected

which I take to mean that the column Years is not present in the csv file ppl_inventory_split_rates, but it is indeed present. When I print ppl_inventory_split_rates though, it does not show a neat table; rather a comma-separated line for headings and comma separated row entries, as below.

>Years.Act_abb_ppl.Power..2000.PP_EX_WB.PP_EX_OTH_ppl..PP_NEW_ppl.PP_IGCC.Total.PP_EX_OTH_ppl...2000.PP_EX_WB.PP_EX_OTH_number.PP_NEW_number.PP_IGCC..Total..PP_TOTAL...........
>1                                                                                                                      >
1990,BC1,0,,0,0,0,0,0,0,0,,,BC1,0,0,0,0,,0,,0,,,,,,,,,,,
>2  1990,BC2,0,,0,0,0,0,0,0,0,,,BC2,0,0,0,0,,0,,0,,,,,,,,,,,
>3  1990,HC1,0,,0,0,0,0,0,0,0,,,HC1,0,0,0,0,,0,,0,,,,,,,,,,,
>4 1990,HC2,0,,0,0,0,0,0,0,0,,,HC2,0,0,0,0,,0,,0,,,,,,,,,,,                                                        
#and so on for 201 rows.

What seems to be the problem here?

I would be grateful for any help on this. (new to R and still figuring stuff out)

NewbietoR
  • 31
  • 1
  • 7
  • 2
    it could be many things, one common issue is that the values of some columns includes commas, which R mistakes for a delimiter. but that's only one possibility. – Mouad_Seridi Nov 26 '18 at 15:46
  • Without code snippets and data examples, your question is too broad. Please take the Stack Overflow tour and read the "How to Ask" section. – StoneGiant Nov 26 '18 at 15:54
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly does the data look like? Can you ask the person what did write the code? – MrFlick Nov 26 '18 at 15:54
  • @MrFlick I have edited the askance. Hope this helps you aid me. The person is no longer available for contact. – NewbietoR Nov 26 '18 at 16:39
  • @StoneGiant Do the edits help in understanding the problem better? – NewbietoR Nov 26 '18 at 16:41
  • 2
    None of the code you show tries to do anything with a CSV file - it's all operating on (presumably) a data frame in your R session. Can you show the code that is used to read the CSV into R? What is the result? Can you post `str(your_data)` immediately after importing it? And the names seem inconsistent. Your code first starts referring to a column named `"year"` - all lower case and singular. Then you get an error with a column named `"`Years"` - capitalized and plural. The snippet of CSV you show doesn't include either `year` or `Years` in its header... – Gregor Thomas Nov 26 '18 at 16:42
  • @Mouad_S Thank you for input. The values do not have commas in them, which is what is confusing me more as to why it is reading it as it is. – NewbietoR Nov 26 '18 at 16:42
  • Okay, thanks for that edit. The argument `sep=" "` tells `read.csv` to expect spaces separating values in the file. It looks like the file follows the standard `csv` convention of commas separating values. If you delete `sep = " "` and let the default take over, it should work. Do look at `str(ppl_inventory_split_rates )` after importing and expect to see `data.frame X obs. of Y variables: ...` where `X` is the number of rows and `Y` is the number of columns. Don't try to run more code until you get that `read.csv` line working right. – Gregor Thomas Nov 26 '18 at 16:47

0 Answers0