简体   繁体   中英

R , Looping through a dataframe , creating a new one with additional content

Caution, quite new to R - but I really would like to do this in R instead of java.

My csv-file (Swedish redlist for species 2020 ) looks like this:

id,svenskt,latin,Organismgrupp,Kategori,Observationer,Landskapstyp,status_abbrev,Rodlistekriterium
249012,,Abia candens,stekel,Art,3,"Jordbrukslandskap (J) - Stor betydelse, Skog (S) - Har betydelse",DD,
249014,,Abia lonicerae,stekel,Art,2,Skog (S) - Stor betydelse,DD,
261452,,Abia nitens,stekel,Art,0,Jordbrukslandskap (J) - Stor betydelse,DD,
  • The whole csv-file can be download from SLU by pressing the button 'skapa csv-fil'.

The interesting columns for me is only the 'id' and the 'status_abbrev' columns. I would like to use those columns to update my db-table, doing something like this:

sql<- paste("update redlist SET status_abbrev='",abbrev,"' ","where id=",id,sep="")

reading the csv-file with this command:

library(dplyr)
redlist <- read.csv("rodlistade_arter_tampered_2.csv",header=TRUE);
dat <- select(redlist,'id', 'status_abbrev')

the output from the 3 first lines would be:

  1. redlist is a dataframe, contains the csv with header.
  2. dat is a dataframe, contains a subset of redlist (id and status_abbrev).

But which library would be best to iterate through the 'dat' data-frame to be able to create something like this? iterating and picking out abbrev and id and creating the below string for each row - (in the end I would like to write these strings to an sql-batch file and update the roughy 5660-records)

sql<- paste("update redlist SET status_abbrev='",abbrev,"' ","where id=",id,sep="")

so that my resulting string would be like this (then iterating through the whole file):

update redlist SET status_abbrev=DD where id=249012

screenshot of redlist and dat -

best,i

Using dplyr::mutate() and glue::glue() you can create the strings like this

library(tidyverse)
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse

str <- 'id,svenskt,latin,Organismgrupp,Kategori,Observationer,Landskapstyp,status_abbrev,Rodlistekriterium
249012,,Abia candens,stekel,Art,3,"Jordbrukslandskap (J) - Stor betydelse, Skog (S) - Har betydelse",DD,
249014,,Abia lonicerae,stekel,Art,2,Skog (S) - Stor betydelse,DD,
261452,,Abia nitens,stekel,Art,0,Jordbrukslandskap (J) - Stor betydelse,DD,'

df <- read_csv(str)

df2 <- df %>% 
  mutate(sql_string = glue("update redlist SET status_abbrev='{status_abbrev}' where id={id}"))

df2
#> # A tibble: 3 x 10
#>       id svenskt latin Organismgrupp Kategori Observationer Landskapstyp
#>    <dbl> <lgl>   <chr> <chr>         <chr>            <dbl> <chr>       
#> 1 249012 NA      Abia… stekel        Art                  3 Jordbruksla…
#> 2 249014 NA      Abia… stekel        Art                  2 Skog (S) - …
#> 3 261452 NA      Abia… stekel        Art                  0 Jordbruksla…
#> # … with 3 more variables: status_abbrev <chr>, Rodlistekriterium <lgl>,
#> #   sql_string <glue>

df2 %>% pull(sql_string)
#> update redlist SET status_abbrev='DD' where id=249012
#> update redlist SET status_abbrev='DD' where id=249014
#> update redlist SET status_abbrev='DD' where id=261452

Created on 2020-07-27 by the reprex package (v0.3.0)

Is this what you are looking for?

For database integration, have a look at DBI .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM