Search code examples
rvector

How to create a vector of 2 elements?


For a function I want to utilize the documentation states that I need to create a vector of 2 elements.

library("dplyr")

df <- data.frame(
  Genus = c("Panthera", "Quercus", "Rosa", "Acer", "Canis"),
  Species = c("leo", "robur", "rubiginosa", "saccharum", "lupus"),
  Common_Name = c("Lion", "English Oak", "Rusty Rose", "Sugar Maple", "Wolf"),
  Latitude = c(1.2921, 51.5074, 48.8566, 45.4215, 37.7749),
  Longitude = c(36.8219, -0.1278, 2.3522, -75.6972, -122.4194)
)


I tried:

latlong<- c(df$Latitude,df$Longitude)

However that just gives me one long vector and I need vector pairs.

I also tried:

latlong<- c("Latitude","Longitude")

however that just gives me a string of 2 characters.

Its probably an easy solution but I don't know how to go about accomplishing it.

Edit: The documentation can be found here: https://cran.r-project.org/web/packages/divvy/divvy.pdf

The function I am specifically looking at is bandit.

In terms of "a vector of two elements", I'm unsure what the output should look like those are just the exact words in the documentation.

I just tried:

df[,c("Latitude", "Longitude")]

which I thought was what I needed but it returned the following error:

Error in .subset(x, j) : invalid subscript type 'list'

In which I attempted to use as.vector() and recieved the same error


Solution

  • The xy param is described in the linked documentation as:

    A vector of two elements, specifying the name or numeric position of columns in dat containing coordinates, e.g. longitude and latitude

    One example in the documentation is:

    ...
    obs <- data.frame(x, y, sp)
    ...
    sdSumry(obs, xy = c('x','y'), ...
    

    Given the names in your frame, I suspect that:

    xy = c('Latitude', 'Longitude')
    

    is what's expected here.