Hi all, I have three columns. Resistance, Time and Frequency, and I need to find correlations between Resistance and Time by each of 100 Frequencies. I have set Frequencies to be a factor. Time is in the format "%m/%d/%Y %H:%M:%S", but correlations dont seem to like Time data. I can run this code from Stackoverflow library(plyr) g <- data.frame(col1=rnorm(100, 1, 1), col2=rnorm(100, 10, 3), col3=c(rep("a", 50), rep("b", 50))) co <- ddply(g, .(col3), function(adf) cor(adf[,1], adf[,2])) But if one of the columns is Time, it does not work. So the functionality of giving correlations by Frequency works, but not when Time is used. I hope this is clear enough. keith M. Keith Cox, Ph.D. Principal MKConsulting 17415 Christine Ave. Juneau, AK 99801 U.S. 907.957.4606 [[alternative HTML version deleted]]
On Tue, 26 Apr 2022 20:13:55 -0800 Marlin Keith Cox <marlinkcox at gmail.com> wrote:> cor(adf[,1], adf[,2])> But if one of the columns is Time, it does not work.Giving you an error that the column "must be a number", right? Which class is your time column now? There are different numeric representations for time. As long as they only vary by the offset (did time start on 1970-01-01, or January 1, 4713 BC?) and scale (are we measuring seconds or days since epoch?), the correlation should come out the same (subject to rounding errors), but R doesn't automatically perform any conversion to a given representation. If your dates are currently character strings, use Sys.strptime or something from the lubridate package to convert them into date objects. Once you have date objects, convert them to numbers. For example, dates stored as POSIXct objects consist of seconds since 1970-01-01, which you can get by using as.numeric(). -- Best regards, Ivan