How to read first 1000 lines of .csv file into R? [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have very big .csv file, it's around a few GB.
I want to read first few thousand lines of it.
Is there any method to do this efficiently?

Use the nrows argument in read.csv(...)
df <- read.csv(file="my.large.file.csv",nrows=2000)
There is also a skip= parameter that tells read.csv(...) how many lines to skip before you start reading.
If your file is that large you might be better off using fread(...) in the data.table package. Same arguments.

If you're on UNIX or OS/X, you can use the command line:
head -n 1000 myfile.csv > myfile.head.csv
Then just read it in R like normal.

Related

Using .sav files in R without all the hassle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
This is a broader/more-general question, but I find reading .sav files in R to be a nightmare. I use the haven package, but often run into errors due to the format in which .sav files are read (as an ex., of many, it refuses to let me coerce the dbl+lbl format into a numeric data frame).
What I typically do to get around this annoying process is to just save the .sav file as a .csv, then re-read it into R, but I'm sure there's got to be a better way, right?!
n/a this is a more-general question

How to read a bulk number image files in R? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have more than 400 image files in my loacl directory.I want to read these images in r for passing it through XG boost algorithm..My two tries(codes) are is given below
library("EBImage")
img <- readImage("/home/vishnu/Documents/XG_boost_R/Data_folder/*.jpg")
and
library(jpeg)
library(biOps)
myjpg <- readJpeg("/home/vishnu/Documents/XG_boost_R/Data_folder/*.jpg")
It is a bit hard to guess what you want to do exactly, but one way to accomplish loading a lot of files and processing them is via a for-loop like this:
files <- list.files() #create a vector with file names
for(i in 1:length(files)){#loop over file names
load(files[i]) #load .rda-file
#do some processing and save results
}
This structure is generalizable to other cases. Depending on what kind of files you want to load, you will have to replace load(files[i]) with the appropriate command, for instance load.image() from the imager package.

R - subsetting a list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am quite new to R and it would be a big help to find a way to do this.
I have a list of values(just one Column and around 16000 values) and I have to break this list up into smaller packets of 1000 values each. Then save each list as a csv file.
Is there a way of doing this is R?
Thank you in advance,
Dgupta
Something like this:
data <- as.data.frame(list)
groups <- split(1:nrow(data), ceiling(seq_along(1:nrow(data)/1000))
for (i in 1:length(groups)){write.csv(data[groups[1,],file=paste(i,'csv'))}
You can split the vector up by using a colon symbol in the vector index. For example:
> x <- c(10,20,30,40)
> x[1:2]
[1] 10 20
you can then write a vector to a csv by using the write.csv()function. One way to approach this is to write loop that gets 1000 items at a time from the vector and writes it to a csv.

How do I import a character vector in R? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a block of text that looks like this: "gatcctccatatacaacggtatctccacctcaggtttagatctca" and it goes on like that for 5000 characters.
I want to import it into R as a character vector. Should I put it in a .rtf file and try to import that file? And what code do I use in order to import it?
Your problem isn't really about reading the data into R, it's splitting up the string into individual characters.
Reading it in using either of the answers posted:
v <- readLines("your_file.txt")
v <- "gatcc...."
Splitting it up, using strsplit:
v <- strsplit(v, "")[[1]]
If you need to copy the text anyway, simply copy it directly into R:
v <- "gatcctccatatacaacggtatctccacctcaggtttagatctca"
I would save it as a text file and then load it in using the readLines() functions:
character_data <- "your_file.txt"
v<-readLines(character_data)
This is a little bit more complicated than copying and pasting but has the advantage of being reproducible for another person if they want to run the code as well as making it easy to change the string later on.

how to keep a large number of data.frame in .csv [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There are a large number of data.frame (more than 50). How can I save them quickly in .csv?
write.csv()
50 lines of code, it's awful...
Help me, guys!
If I understand the many data.frames may be available in in your R session...
First create a vector with the names of the data.frames... use ls or some thing similar. Then use get to get the R object after the names (the data.frames in this case)
myfiles <- ls ()
Then
for (d in myfiles) {
current <- get (d)
write.csv (current, filename = paste0 (d, ".csv"))
}

Resources