0

I'm new to R and got a assignment to do some basic research with the use of R I have a csv file imported with data of wind direction and wind speed and want to split the wind speed based on direction

So i created this bit of R code

north.ls = list()
east.ls  = list()
south.ls = list()
west.ls = list()
i = as.integer(1)
print("start")
for (i in 1:length(DD)) {
  if (DD[i] >=315 & DD[i] <= 360 | DD[i] >= 1 & DD < 45) {
    north.ls[[i]] = as.integer(FH[i])
    print("nord")
  }
  if(DD[i] >=45 & DD[i] < 135){
    east.ls[[i]] = as.integer(FH[i])
    print("east")
  }
  if(DD[[i]] >= 145 & DD[i] < 225){
    south.ls[[i]] = as.integer(FH[i])
    print("south")
  } 
  if(DD[[i]] >=225 & DD[i] < 315){
    west.ls[[i]] = as.integer(FH[i])
    print("west")
  }
}

this works fine at puts the right speeds in the right lists but every time the condition is not correct the list still gets a null value so I have a lot of null values in the lists. What is the problem and how can I fix it?

I hope you understand my explanation

thanks in advance

0romis
  • 3
  • 1
  • Do you have a column for direction in your dataset (i.e. how do you know which rows belong to N/E/S/W)? – Mako212 Oct 09 '18 at 18:49
  • Hi 0romis. This isn't really R code. You're missing out on all the vectorizing goodness by using for an ifs like this. It looks like you're overly influenced by other coding language (e.g. initializing i). Maybe go back to your earlier tutorials and think how this can be done in vectorized fashion? – iod Oct 09 '18 at 18:53
  • When asking for help, you should 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. There are likely better ways to accomplish what you are after. – MrFlick Oct 09 '18 at 18:56
  • 1
    When you create a new item on a list at position [i] without items in previous positions, all those positions get NULLs. One solution would be to replace `north.ls[[i]] = as.integer(FH[i])` with `north.ls=c(north.ls,FH[i])`. But again - this is NOT a good way of writing R code. – iod Oct 09 '18 at 19:04
  • 1
    The real problem for persons attempting an answer is that we don't know what uses these direction lists will be put to. If you just want a count that could be handled better with a table-call. If you want the distribution of speeds then look for the package `circular` which has facilities for calculating and displaying data that is arranged around 360 degrees. You may have set up an "XY problem": See https://stackoverflow.com/search?q=%22xy+problem%22 – IRTFM Oct 09 '18 at 22:42

1 Answers1

1

When you create a new item on a list at position [i] without items in previous positions, all those positions get NULLs.

Here's a slightly better way of producing what you're trying to do (I'm making some educated guesses about your data structure and your goals), without introducing these NULLs:

north.ls<-FH[(DD>=315 & DD <= 360) | (DD >= 1 & DD < 45)]
east.ls<-FH[DD>=45 & DD < 135]
south.ls<-FH[DD>=135 & DD < 235]
west.ls<-FH[DD>=235 & DD < 315]

This will give you four vectors that divide the data in FH into north, east, south, and west based on the data in DD. The length of each of the four lists is NOT equal to the length of FH or DD (or each other), and there should be no NULLs introduced unless they're already in FH.

iod
  • 7,412
  • 2
  • 17
  • 36