Skip to content
Prev 359934 / 398503 Next

Generating random data with non-linear correlation between two variables

Hello,


I changed the last 3 lines from v1 and v2 to myData$v1 and myData$v2.

Creating the vectors in not a problem.
Where you will run into issues is when you create the data.frame.
The length of v1 is 1001 and the length of v2 is 651.
The warning message below is telling us that the lengths of v1 and v2 
are different.
"Shorter vectors are recycled as often as need be until they match the 
length of the longest vector."

See section 2.2 of this document for more on recycling.
https://cran.r-project.org/doc/manuals/R-intro.pdf

    v1 <- sort(rnorm(500:1500, mean = 1100, sd = 1), decreasing = F)
    v2 <- sort(rnorm(300:950, mean = 400, sd = 1), decreasing = F)
    length(v1)
    length(v2)
    myData <- data.frame(cbind(v1, v2))

         Warning message:
          In cbind(v1, v2) :
         number of rows of result is not a multiple of vector length (arg 2)

    plot(myData$v1, myData$v2, type = "l")

    cor(myData$v1, myData$v2, method = "pearson")

    plot(myData, type = 'l')

Michael Long
On 04/08/2016 11:57 AM, Muhammad Bilal wrote: