readr

How can I write dplyr groups to separate files?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 17:44:41
问题 I'm trying to create separate .csv files for each group in a data frame grouped with dplyr's group_by function. So far I have something like by_cyl <- group_by(mtcars, cyl) do(by_cyl, write_csv(., "test.csv")) As expected, this writes a single .csv file with only the data from the last group. How can I modify this to write multiple .csv files, each with filenames that include cyl? 回答1: You can wrap the csv write process in a custom function as follows. Note that the function has to return a

How to make R's read_csv2() recognise the text characters properly

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 15:20:13
Trying to read a csv file using read_csv2() from readr package. The problem is read_csv2() doesn't recognise the characters properly while R's default read.csv2 successfully does. For example: the original value: KOZYATAĞI how read_csv2() recognises: KOZYATA<'d0'>I I have checked the help file and also tried below listed coding; however couldn't make it happen. 1st try: ended up with wrong characters my_df <- read_csv2("my_path/my_file.csv") 2nd try: manually state the encoding. my_df <- read_csv2("my_path/my_file.csv", locale(encoding = "UTF-8")) Error: col_names must be TRUE, FALSE or a

Install package without internet: error in install.packages: no such file or directory

落爺英雄遲暮 提交于 2019-11-28 11:52:07
问题 I'm trying to install the readr package in R on a laptop without internet connection using the install.packages("C:/Users/MVC/Desktop/readr-master.zip", repos=NULL) command, but I keep getting the same error: error in install.packages : cannot open file 'C:/Program Files/R/R-3.3.0/library/file27f07de57168 /readr-master/R/POSIXct.R': No such file or directory Anybody who know why this is or how I can install the package without an internet connection? 回答1: This package is available as tar.gz

Write xml-object to disk

烈酒焚心 提交于 2019-11-28 10:32:18
I have a big bunch of xml -files, which I need to process. For that matter I want to be able to read the files, and save the resulting list of objects to disk. I tried to save the list with readr::write_rds , but after reading it in again, the object is somewhat modified, and not valid any more. Is there anything I can do to alleviate this problem? library(readr) library(xml2) x <- read_xml("<foo> <bar>text <baz id = 'a' /></bar> <bar>2</bar> <baz id = 'b' /> </foo>") # function to save and read object roundtrip <- function(obj) { tf <- tempfile() on.exit(unlink(tf)) write_rds(obj, tf) read

How to make R's read_csv2() recognise the text characters properly

北城以北 提交于 2019-11-28 08:50:38
问题 Trying to read a csv file using read_csv2() from readr package. The problem is read_csv2() doesn't recognise the characters properly while R's default read.csv2 successfully does. For example: the original value: KOZYATAĞI how read_csv2() recognises: KOZYATA<'d0'>I I have checked the help file and also tried below listed coding; however couldn't make it happen. 1st try: ended up with wrong characters my_df <- read_csv2("my_path/my_file.csv") 2nd try: manually state the encoding. my_df <- read

Write xml-object to disk

前提是你 提交于 2019-11-27 04:39:32
问题 I have a big bunch of xml -files, which I need to process. For that matter I want to be able to read the files, and save the resulting list of objects to disk. I tried to save the list with readr::write_rds , but after reading it in again, the object is somewhat modified, and not valid any more. Is there anything I can do to alleviate this problem? library(readr) library(xml2) x <- read_xml("<foo> <bar>text <baz id = 'a' /></bar> <bar>2</bar> <baz id = 'b' /> </foo>") # function to save and

How do you read in multiple .txt files into R? [duplicate]

纵饮孤独 提交于 2019-11-26 01:39:51
问题 This question already has an answer here: How to import multiple .csv files at once? 10 answers I\'m using R to visualize some data all of which is in .txt format. There are a few hundred files in a directory and I want to load it all into one table, in one shot. Any help? EDIT: Listing the files is not a problem. But I am having trouble going from list to content. I\'ve tried some of the code from here, but I get a bug with this part: all.the.data <- lapply( all.the.files, txt , header=TRUE)