R paste input in a way that each row a string is - r

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"

Related

Convert character vector to a single string with line breaks, save as an object in R

I am trying to use R to take the contents of a character vector and create a single string with line breaks between the items. The ultimate goal is to import the text into an excel file, such that one cell contains all of the items, each one on a separate line.
TextVector <- c("Name1", "Name2", "Name3")
cat(paste(TextVector, collapse = '\n'))
This gives the output I want, but it's only printed...I cannot save it to an object. In the end, I want a csv file that will have the items contained in one cell with line breaks, that will appear like this:
Name1
Name2
Name3
Thank you for any suggestions!
If you want to save the object into disk, you have several choices.
save() or save.image(). The object or workspace will be saved as an .RData file. It can be loaded using load(), like load("/path/to/file.RData")
write to file, i.e, write.table or write.csv
can be csv, excel, tsv
if you want in xlsx format, you can use xlsx library
library(xlsx)
write.xlsx(mydata, "c:/mydata.xlsx")

Read CSV and assign values to r variables

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

Read text file using R into one column

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.

Inconsistency between 'read.csv' and 'write.csv' in R

The R function read.csv works as the following as stated in the manual: "If there is a header and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names." That's good. However, when it comes to the function write.csv, I cannot find a way to write the csv file in a similar way. So, if I have a file.txt as below:
Column_1,Column_2
Row_1,2,3
Row_2,4,5
Then when I read it using a = read.csv('file.txt'), the row and column names are Row_x and Column_x as expected. However, when I write the matrix a to a csv file again, then what I get as a result from write.csv('file2.txt', quote=F) is as below:
,Column_1,Column_2
Row_1,2,3
Row_2,4,5
So, there is a comma in the beginning of this file. And if I would read this file again using a2 = read.csv('file2.txt'), then resulting a2 will not be the same as the previous matrix a. The row names of the matrix a2 will not be Row_x. That's, I do not want a comma in the beginning of the file. How can I get rid of this comma while using write.csv?
The two functions that you have mentioned, read.cvs and write.csv are just a specific form of the more generic functions read.table and write.table.
When I copy your example data into a .csv and try to read it with read.csv, R throws a warning and says that the header line was incomplete. Thus it resorted to special behaviour to fix the error. Because we had an incomplete file, it completed the file by adding an empty element at the top left. R understands that this is a header row, and thus the data appears okay in R, but when we write to a csv, it doesn't understand what is header and what is not. Thus the empty element only appearing in the header row created by R shows up as a regular element. Which you would expect. Basically it made our table into a 3x3 because it can't have a weird number of elements.
You want the extra comma there, because it allows programs to read the column names in the right place. In order to read the file in again you can do the following, assuming test.csv is your data. You can fix this by manually adding the column and row names in R, including the missing element to put everything in place.
To fix the wonky row names, you're going to want to add an extra option specifying which row is the row names (row.names = your_column_number) when you read it back in with the comma correctly in place.
y <- read.csv(file = "foo.csv") #this throws a warning because your input is incorrect
write.csv(y, "foo_out.csv")
x <- read.csv(file = "foo.csv", header = T, row.names = 1) #this will read the first column as the row names.
Play around with read/write.csv, but it might be worth while to move into the more generic functions read.table and write.table. They offer expanded functionality.
To read a csv in the generic function
y <- read.table(file = "foo.csv", sep = ",", header = TRUE)
thus you can specify the delimiter and easily read in excel spreadsheets (separated by tab or "\t") or space delimited files ( " " ).
Hope that helps.

Saving a txt file as a delimited csv file in R

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.

Resources