Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert CSV to Out object #211

Open
lrocax opened this issue May 17, 2020 · 15 comments
Open

Convert CSV to Out object #211

lrocax opened this issue May 17, 2020 · 15 comments

Comments

@lrocax
Copy link

lrocax commented May 17, 2020

Hi,

Great package.

A question: I have a CSV that contain 4 columns (objectName,Date,Location,Coord) with several rows. How can I import the objectName and Coord as Out object?

Ex1.zip

Thanks

@vbonhomme
Copy link
Collaborator

vbonhomme commented May 17, 2020 via email

@vbonhomme
Copy link
Collaborator

vbonhomme commented May 17, 2020 via email

@lrocax
Copy link
Author

lrocax commented May 18, 2020

It is an output from a python code that I wrote that is structured as followed [x1 y1; x2 y2; …xn yn]. So, a general solution for CSV file will be awesome.

@petrpajdla
Copy link

Generally speaking, the structure you showed in your example is not a simple CSV file, it resembles file structures used with spatial data. I think you first need to read the coordinates and add the metadata to an object with coordinates in the next step...

@vbonhomme
Copy link
Collaborator

vbonhomme commented May 18, 2020 via email

@raz1
Copy link

raz1 commented Jun 23, 2021

I was running this code using R 4.0.5

library(tidyverse)
library(Momocs)
 
df <- read.csv(file = 'E:/Ex1.csv')
  
from_csv_to_Out <- function(df){
  df <- as_tibble(df) # cosmetics
  fac <- df %>% select(-matches("X[[:digit:]]+(x|y)"))     # all but non
  coord columns -> our $fac
  coo <- df %>% select(matches("X[[:digit:]]+(x|y)")) %>%  # all coord
    columns         -> our $coo
  #split rowwise
  split(1:nrow(.)) %>%
    # for each row, turn columns into shape
    map(~.x %>%
          # select appropriate columns
          select(matches("X[[:digit:]]+(x|y)")) %>%
          # unlist and turn into a shape
          unlist() %>%
          na.omit() %>% # required because some missing data
          matrix(ncol=2, byrow=T))
  # build  and return the Out
  Out(coo, fac)
}

and I got this output:

library(tidyverse)
-- Attaching packages ------------------------------------------ tidyverse 1.3.1 --
<U+221A> ggplot2 3.3.4     <U+221A> purrr   0.3.4
<U+221A> tibble  3.1.2     <U+221A> dplyr   1.0.6
<U+221A> tidyr   1.1.3     <U+221A> stringr 1.4.0
<U+221A> readr   1.4.0     <U+221A> forcats 0.5.1
-- Conflicts --------------------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
> library(Momocs)

Attaching package: ‘Momocs’

The following objects are masked from ‘package:dplyr’:

   arrange, combine, filter, mutate, rename, sample_frac, sample_n,
   select, slice

The following object is masked from ‘package:tidyr’:

   chop

The following object is masked from ‘package:stats’:

   filter

> df <- read.csv(file = 'E:/Ex1.csv')
> from_csv_to_Out <- function(df){
+   df <- as_tibble(df) # cosmetics
+   fac <- df %>% select(-matches("X[[:digit:]]+(x|y)"))     # all but non
+   coord columns -> our $fac
Error: unexpected symbol in:
"  fac <- df %>% select(-matches("X[[:digit:]]+(x|y)"))     # all but non
 coord columns"
>   coo <- df %>% select(matches("X[[:digit:]]+(x|y)")) %>%  # all coord
+     columns         -> our $coo
Error in columns(.) : could not find function "columns"
>   #split rowwise
>   split(1:nrow(.)) %>%
+     # for each row, turn columns into shape
+     map(~.x %>%
+           # select appropriate columns
+           select(matches("X[[:digit:]]+(x|y)")) %>%
+           # unlist and turn into a shape
+           unlist() %>%
+           na.omit() %>% # required because some missing data
+           matrix(ncol=2, byrow=T))
Error in nrow(.) : object '.' not found
>   # build  and return the Out
>   Out(coo, fac)
Error in Out(coo, fac) : object 'coo' not found
> }
Error: unexpected '}' in "}"

Any ideas how to solve the problem?

@vbonhomme
Copy link
Collaborator

vbonhomme commented Jun 23, 2021 via email

@raz1
Copy link

raz1 commented Jun 23, 2021

Thanks for the super fast respond.

I tried this line with this output:

> ex1 <- from_csv_to_Out(df)
 Error in matrix(., ncol = 2, byrow = T) : 
  'data' must be of a vector type, was 'NULL' 

I did something wrong?

@vbonhomme
Copy link
Collaborator

vbonhomme commented Jun 23, 2021 via email

@raz1
Copy link

raz1 commented Jun 23, 2021

The same error

> ex1 <- from_csv_to_Out('E:/Ex1.csv')
 Error in matrix(., ncol = 2, byrow = T) : 
  'data' must be of a vector type, was 'NULL' 

@vbonhomme
Copy link
Collaborator

vbonhomme commented Jun 23, 2021 via email

@vbonhomme
Copy link
Collaborator

vbonhomme commented Jun 23, 2021 via email

@raz1
Copy link

raz1 commented Jun 23, 2021

It is the file given above by lrocax

@vbonhomme
Copy link
Collaborator

vbonhomme commented Jun 23, 2021 via email

@raz1
Copy link

raz1 commented Jun 23, 2021

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants