-3

I have compass reading values (0 to 360º) in 6 out of 73 columns in my data frame, and all these columns have NA's in some cells. How do I convert multiple columns of a data.frame in bulk to a circular class/type?

Zong
  • 6,160
  • 5
  • 32
  • 46
  • please provide a reproducible example so people can test and help you. – alittleboy May 14 '14 at 14:59
  • There are two possible questions here and I'm not sure which you want. Are you looking for a existing class to use for this type of data? (That question would get closed as off topic because it is a tool recommendation.) If so, check out the `circular` class in the `circular` package. Alternatively, are you asking how to convert multiple columns of a data.frame in bulk to a specific class/type? If that is the question, there are likely already answers around here about that. – Brian Diggs May 14 '14 at 15:16
  • @ Brian Diggs, thank you for your comment! I am asking how to convert multiple columns of a data.frame in bulk to a specific class/type (circular). I could not find this topic in a way I could apply it to my case, sorry. I must say this is a bit of a nightmare to me. I appreciate you taking time to reply. – user3636078 May 14 '14 at 15:29

1 Answers1

1
# create sample dataset - something you should have done!
set.seed(1)                                   # for reproducible example
df <- data.frame(matrix(rnorm(7300),ncol=73)) # 73 columns, 100 rows
circ.cols <- sample(1:73,6)                   # six columns with degree data
df[,circ.cols] <- data.frame(matrix(sample(0:360,600,replace=T),ncol=6))

# you start here - assumes the 6 columns are identified in circ.cols
library(circular)
for (i in circ.cols) df[,i] <- as.circular(df[,i],units="degrees")

In future, you are unlikely to get help unless you put in the effort to create a sample dataset. As you can see, it's very easy to do that.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • jlhoward - I appreciate very much your help! Thank you! I tried to give examples by posting a table as an image but was not allowed by the system and didn't figure out how to do it. I am the opposite of a programmer and lack these skills by nature. I think your reply will save me but still need to study/try it :) Thank you once again! – user3636078 May 15 '14 at 15:35
  • You're welcome. For future reference, don't post data as an image - this makes it impossible to cut and paste into an R session, so it's useless. Rather type `str(mydata)` and post the output of that in a code block. If it's a large file and you can't generate a representative example, upload the file somewhere (e.g. Dropbox) and post a link in your question. [Read this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more information. Finally, since you're new to SO, [read this](http://stackoverflow.com/help/someone-answers). – jlhoward May 15 '14 at 19:26