[英]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.小心,对 R 来说很新——但我真的很想在 R 而不是 java 中执行此操作。
My csv-file (Swedish redlist for species 2020 ) looks like this:我的 csv 文件(2020 年物种的瑞典红名单)如下所示:
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 interesting columns for me is only the 'id' and the 'status_abbrev' columns.对我来说有趣的列只有“id”和“status_abbrev”列。 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:使用以下命令读取 csv 文件:
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:前 3 行中的 output 将是:
redlist
is a dataframe, contains the csv with header. redlist
是 dataframe,包含 csv 和 header。dat
is a dataframe, contains a subset of redlist (id and status_abbrev). dat
是 dataframe,包含 redlist 的子集(id 和 status_abbrev)。But which library would be best to iterate through the 'dat' data-frame to be able to create something like this?但是哪个库最适合遍历“dat”数据框以创建类似的东西? 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)迭代并挑选 abbrev 和 id 并为每一行创建以下字符串- (最后我想将这些字符串写入 sql-batch 文件并更新粗糙的 5660 记录)
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 - redlist 和 dat 的屏幕截图-
best,i最好的,我
Using dplyr::mutate()
and glue::glue()
you can create the strings like this使用dplyr::mutate()
和glue::glue()
你可以创建这样的字符串
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)由代表 package (v0.3.0) 于 2020 年 7 月 27 日创建
Is this what you are looking for?这是你想要的?
For database integration, have a look at DBI .对于数据库集成,请查看DBI 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.