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.
Related
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 5 years ago.
Improve this question
I would like to classify pictures by using R software. I have no knowledge in image processing so it will be great if someone would point me to the right direction. First, how can I turn a picture that I have into a structured dataframe so I'll be able to analyze it? Is there a package for this task? Second, Is there a defined process for picture classification that I can follow?
With this lines you can load images in R and store them in a list.
library(jpeg)
setwd("path/to/folder/with/images/")
pics <- dir()
all_images <- NULL
for(z in pics){
all_images[[z]] <- readJPEG(z, native = T)
}
# check the first image in the list
plot(1:2, type='n')
rasterImage(all_images[[1]],1,1,2,2)
for processing images you can use the imager package.
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.
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"))
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to pass a data frame to a function as an argument. And then inside the function, I want to work on different combinations of columns for graphical presentation. Basically, I want to do graphical presentation on different data files. I want that, I pass the data file as an argument and then get the graphs. How can I do this in R.
You are not giving us much info but here is a very basic starting point:
library(ggplot2) # if you don't have this library run install.packages('ggplot2')
myAmazingFunction <- function(myDF) {
ggplot(myDF,aes(X,Y))+geom_line()
}
df <-data.frame(X=1:30, Y=runif(30), Z=1.3*runif(30))
myAmazingFunction(df)
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.