I'm new to R, and I wonder how to read a csv file and assign the value from the csv file to a variable? For example I have a csv file and I want to assign filename and filepath to R variables. I know how to read csv into R variable with
mydata <- read.csv("testing.csv")`
But how to assign value from Filename which is 'globaldata.txt' and Filepath which is 'E:\Test\Global' to r variable
variable value
Filename globaldata.txt
Filepath E:\Test\Global
it's safe to use read.table and define the class for each variable in the argument, see the help file ?read.table
mydata <- read.table("testing.csv", colClasses = c("character", "character"))
The return value mydata will be a data frame, and u can simply extract what you want using the $ sign
e.g.
value1 <- mydata$column1
etc.
You can do the following :
Filename<-"globaldata.csv" # if this is a csv and not a .txt file
Filepath<-"E:/Test/Global/" # if you are on Windows you need to use "/"
which then allows you to do (if this is what you want)
mydata<-read.csv(paste0(Filepath,Filename))
EDIT
If I understand correctly you have a csv file named testing.csv with two columns: one with Filenames and one with Filepaths.
In that case when you have mydata<-read.csv("testing.csv")you have a dataframe with two columns. To access the first one you use mydata[,1] and for the second (Filepath) : mydata[,2]. If you want the Filename of the third entry in the file you then use mydata[3,1](before the comma is the row, after is the column)
I hope this is what you are looking for, otherwise I'm afraid I misunderstood you again. Having a look at the csv file will help to better understand the question
Related
I have many files in my working directory with the same name followed by a number as such "name_#.csv". They each contain the same formatted time series data.
The file names are very long so when I import them into dataframes the df name is super long and I'd like to rename each as "df_#" so that I can then create another function to plot each individually or quickly plot, for example, the first three without typing in this megalong name.
I don't want to concatenate anything to the current names, but take each one and rename it completely ending with the number in the list as it iterates through the files.
Here is an example I have so far.
name = list.files(pattern="*.csv")
for (i in 1:length(name)) assign(paste0("df", name[i]), read.csv(name[i], skip = 15 ))
This is just adding a 'df' to the front and not changing the whole name.
I'm also not sure if it makes sense to proceed this way. Essentially my data is three replicates of time series data on the same sample and I eventually want to take three at a time and plot them on the same graph and so forth until the end of the files.
You can name the file in your global environment without renaming the original file in the folder by just telling R you want to assign it that name in the loop with a few modifications to your original code. For instance:
# Define file path to desired folder
file_path <- "Desktop/SO Example/" #example file path, though could eb working directory
# Your code to get CSV file names in the folder
name <- list.files(path = file_path, pattern="*.csv")
# Modify the loop to assign a new name
for(x in seq_along(name)){
assign(paste0("df_", x),
read.csv(paste0(file_path, name[x]), skip = 15))
}
This will load the data as df_1, df_2, etc. I believe in your assign function you were using paste0("df", name[i]), which concatenates "df" with the filename in position i, not the value of I in the loop - that is why you were getting df prepended to each name on import.
I have an Excel with data, like this (but then N=1.000):
p_evar7_CO.main.
p_evar7_CP.acquistion..sign_up.start
p_evar7_CP.main.
p_evar7_CP.main.facial_stylers00
I want to put it in a vector, but with simple copy/pasting it goes wrong. I want this as result:
Excel <- c("p_evar7_CO.", "p_evar7_CP.acquistion..sign_up.start", "p_evar7_CP.main.","p_evar7_CP.main.facial_stylers00")
So basically: How can I paste a big data file into R, and automatically separate it with a Comma and Quote each row?
EDIT I don't want to load in an Excel data file, but only pasting columns names (and have them as a vector).
Looks like you could do a simple scan().
scan(file, what = "")
where file is your file name as a character string. If you are working with copied text, then you can enter "clipboard" as the file name.
scan("clipboard", what = "")
For example, I copied the file text from your question for the following code.
scan("clipboard", what="")
# Read 4 items
# [1] "p_evar7_CO.main." "p_evar7_CP.acquistion..sign_up.start"
# [3] "p_evar7_CP.main." "p_evar7_CP.main.facial_stylers00"
Apologies if this is a trivial question. I saw others like it such as: How can I turn a part of the filename into a variable when reading multiple text files into R? , but I still seem to be having some trouble...
I have been given 50000 .txt files. Each file contains a single observation (a single row of data) with exactly 12 variables (number of columns). The name of each .txt file is fairly regular. Specifically, each .txt file has a code at the end indicating the type of observation across three dimensions. An example of this code is 'VL-VL-NE' or 'VL-M-N' or 'H-H-L' (not including the apostrophes). Therefore, an example of a file name could be 'I-love-using-R-20_01_2016-VL-VL-NE.txt'.
My problem is that I want to include this code at the end of the .txt file in the actual vector itself when I import into R, i.e., I want to add three more variables (columns) at the end of the table corresponding to the three parts of code at the end of the file name.
Any help would be greatly appreciated.
Because you have exactly the same number of columns in each file, why don't you import them into R using a loop that looks for all .txt files in a particularly directory?
df <- c()
for (x in list.files(pattern="*.txt")) {
u<-read.csv(x, skip=6)
u$Label = factor(x) #A column that is the filename
df <- rbind(df,u)
}
You'll note that the file name itself becomes a column. Once everything is into R, it should be fairly easy to use a regex function to extract the exact elements you need from the file name column (df$Label).
I'm using:
x<-read.table(file,sep="")
in order to read a space-delimited numbers from a .txt file, but I receive the data back in multiple columns because the text file contains multiple lines (whose data is no different but of the same type).
How can I read all of the numbers in the different lines into one column only?
You can use ?scan:
x <- scan(file, what = "numeric")
or something simiar, depending on the structure of your file, should work. You might need to check / adjust the sep parameter.
Description of scan:
Read data into a vector or list from the console or file.
If you want x as a column in a data.frame, you can do
dat <- data.frame(x)
afterwards.
I have the following code to read a file and save it as a csv file, I remove the first 7 lines in the text file and then the 3rd column as well, since I just require the first two columns.
current_file <- paste("Experiment 1 ",i,".cor",sep="")
curfile <- list.files(pattern = current_file)
curfile_data <- read.table(curfile, header=F,skip=7,sep=",")
curfile_data <- curfile_data[-grep('V3',colnames(curfile_data))]
write.csv(curfile_data,curfile)
new_file <- paste("Dev_C",i,".csv",sep="")
new_file
file.copy(curfile, new_file)
The curfile thus hold two column variables V1 and V2 along with the observation number column in the beginning.
Now when I use file.copy to copy the contents of the curfile into a .csv file and then open the new .csv file in Excel, all the data seems to be concatenated and appear in a single column, is there a way to show each of the individual columns separately? Thanks in advance for your suggestions.
The data in the .txt file looks like this,
"","V1","V2","V3"
"1",-0.02868862,5.442283e-11,76.3
"2",-0.03359281,7.669754e-12,76.35
"3",-0.03801883,-1.497323e-10,76.4
"4",-0.04320051,-6.557672e-11,76.45
"5",-0.04801207,-2.557059e-10,76.5
"6",-0.05325544,-9.986231e-11,76.55
You need to use Text to columns feature in Excel, selecting comma as a delimiter. The position of this point in menu depends on version of Excel you are using.