Error: incorrect number of dimensions

Hello,

I am getting the following error when trying to add a new variable from a vector matrix onto my data frame:

hs_directory <- hs_directory %>% mutate(lat = lat_long[,1], long = lat_long[,2])

There's an error coming up: Error in lat_long[, 1] : incorrect number of dimensions

To see if my coding was wrong, I copied/pasted from the course I took the data from, but the same error message appears. Any suggestion?

Thanks!

The error message suggests that lat_long doesn't have the same number of rows than hs_directory but we can't know fore sure since you are not providing sample data.

Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

Hi!

I am having troubles trying to put my code into a reprex format. When I run the df_paste function I can easily create a new data frame, but when I run the reprex function on my sample data together with the code that's currently giving me trouble, the following message comes up:

It seems reprex cannot find the new sample data! However this message doesn't come up when using the iris data table:

Any idea why? What am I doing wrong?

You have to create the sample dataframe in the reprex itself, and paste the output of reprex() here directly not with screenshots, see this example:

  • If you run reprex() like this
reprex({
    # Create the sample dataset with the output of datapasta::df_paste()
    sample_data <- data.frame(
        Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
        Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9),
        Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7),
        Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4),
        Species = as.factor(c("setosa", "setosa", "setosa", "setosa", "setosa",
                              "setosa")))
    
    # Now you can use the dataset in your reprex
    summary(sample_data)
})
  • You get this result ready to be posted in the forum.
# Create the sample dataset with the output of datapasta::df_paste()
sample_data <- data.frame(
    Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
    Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9),
    Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7),
    Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4),
    Species = as.factor(c("setosa", "setosa", "setosa", "setosa", "setosa",
                          "setosa")))

# Now you can use the dataset in your reprex
summary(sample_data)
#>   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width    
#>  Min.   :4.600   Min.   :3.000   Min.   :1.300   Min.   :0.2000  
#>  1st Qu.:4.750   1st Qu.:3.125   1st Qu.:1.400   1st Qu.:0.2000  
#>  Median :4.950   Median :3.350   Median :1.400   Median :0.2000  
#>  Mean   :4.950   Mean   :3.383   Mean   :1.450   Mean   :0.2333  
#>  3rd Qu.:5.075   3rd Qu.:3.575   3rd Qu.:1.475   3rd Qu.:0.2000  
#>  Max.   :5.400   Max.   :3.900   Max.   :1.700   Max.   :0.4000  
#>    Species 
#>  setosa:6  
#>            
#>            
#>            
#>            
#> 

Created on 2019-06-23 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.