I have an xls. file that contains the following information
I want to delete from R columns D and E and of course that the data of F moves to the left. How I can do that in R?
Thanks
Read the .xls file into R with one of the several packages (eg. xlsReadWrite:read.xls()), delete the columns :
data$Column <- NULL
or
data <- data[ ,-c(4,5)]
and then write the new data to a .xls file with one of the mentioned packages.
You can convert it into a csv file and use read.csv function to load it in R. Then, you delete the columns you want to and save it with write.csv.
Related
I have a large number of txt files supplied to me by a third party, each containing two columns of string data. The data format is completely consistent but there are no column headers.
I'm trying to combine them all into one file. The simple way to do this usually is by opening the windows command prompt in the file location and using say copy *.txt MyMergedFile.txt. In this case it copies the contents of the last file on the list to my new file and ignores the others. I assumed that this is because of the lack of headers? Is there a way to either quickly and easily insert headers into all my files, so I can use the usual method, or a simple way of combining these without headers? Happy to use PowerShell, SQL2008, R, vb, whatever has the lowest hassle factor. I'm working in Windows 10. The application is building a large lookup table in a GIS geodatabase.
I fixed this in R using the following:
#get tidyverse
library(tidyverse)
# make a list of target files, i.e. all the .txts in my designated folder
files<-list.files(path = "C:/temp/myfiles", pattern = "*.txt", full.names = T)
# quick check to make sure they are all there
print files
# put the contents into one file
masterfile<- sapply(files, read.table, simplify=FALSE) %>% bind_rows()
From here I can read my data into a suitable format for the next stage in my workflow.
I want to read in a bunch of different .csv files from a folder, average the 9th column of each individual one (starting at the 2nd row because I want to exclude the headers), and then output the averages into a new .csv file as a single column list. I have tried some code so far, but Matlab just says it is busy and never reads or outputs anything. Any advice on where to go next? Thanks!
function csv_write_2()
folder = 'C:\Users\Brent\Desktop\MCGOUGH\2017-07-12_bov_da_medmen-l_01\vic-2d_data';
csvFiles = dir(fullfile(folder, '*.csv'));
numfiles = length(csvFiles);
average = zeros(1, numfiles);
for k = 1:numfiles
M = csvread(fullfile(folder, csvFiles(k).name),1,0);
average(k) = mean(M(:,9));
end
csvwrite(fullfile(folder, 'output.csv'), average);
end
Two suggestions:
1) Use the "Import Data" GUI button to generate the script for importing from the CSV. This is in the "Home" tab. You'll click it, then select the data you want to import, the format you want it imported as, etc. Then click the down arrow under "Import Selection" and click "Generate Function". You can then modify this function as needed, and call it in a loop to loop over your various CSV files. This way you'll know you've got the importing part written correctly.
2) Use xlsread instead. This does take longer to run, but is MUCH more intuitive an easy to use. There's also xlswrite, which I haven't used, but I assume is similarly easy.
Is there a quick way to append a zero to some highlighted cells in my csv file so I don't have to go one by one?
Should be 01234
Instead it is showing 1234
Since all the zip codes are different I cant just do a simple find and replace. TY
You can do that using cell formatting option Zip Code.
Select the column/cells containing zip codes.
Right click -> Format Cells
Number tab -> Special category
And select Zip Code type.
You can:
Open the csv in excel
Insert a mask at ZipCode column using =TEXT(A1, "00000")
Save as csv, ignoring the fact that you'll loose the formulas (this will preserve the results in the csv file)
I have multiple CSV files say 5 of them, I want to consoildate all these CSV to a single excel workbook in separate sheets of that Combined excel file.
Please help me.
You did not specify a language or tech. so I understand you have no preferences. I had an article exactly on this using R: http://analystcave.com/r-staring-you-journey-with-r-and-machine-learning/
See an elegant solution in R below:
csv1 <- read.csv(file="testcsv1.csv", sep=";")
csv2 <- read.csv(file="testcsv2.csv", sep=";")
csvRes <- rbind(csv1,csv2)
write.table(csvRes, file="resCsv.csv", sep=";",row.names=FALSE)
How can I save data from an Excel sheet to .RData file in R? I want to use one of the packages in R and to load my dataset as data(dataset) i think i have to save the data as .RData file and then load that into the package. My data currently is in an Excel spreadsheet.
my excel sheets has column names like x, y , time.lag.
I have saved it as .csv
then i use:
x=read.csv('filepath', header=T,)
then i say
data(x)
and it shows dataset 'x' not found
There are also several packages that allow directly reading from XLS and XLSX files. We've even had a question on that topic here and here for example. However you decide to read in the data, saving into an RData can be handled with save, save.image, saveRDS and probably some others I'm not thinking about.
save your Excel data as a .csv file and import it using read.csv() or read.table().
Help on each will explain the options.
For example, you have a file called myFile.xls, save it as myFile.csv.
library(BBMM)
# load an example dataset from BBMM
data(locations)
# from the BBMM help file
BBMM <- brownian.bridge(x=locations$x, y=locations$y, time.lag=locations$time.lag[-1], location.error=20, cell.size=50)
bbmm.summary(BBMM)
# output of summary(BBMM)
Brownian motion variance : 3003.392
Size of grid : 138552 cells
Grid cell size : 50
# subsitute locations for myData for your dataset that you have read form a myFile.csv file
myData <- read.csv(file='myFile.csv', header=TRUE)
head(myData) # will show the first 5 entries in you imported data
# use whatever you need from the BBMM package now ....
Check RODBC package. You can find an example in R Data Import/Export. You can query data from excel sheet as if from a database table.
The benefit of reading Excel sheet with RODBC is that you get dates (if you work with any) in a proper format. With intermediate CSV, you'd need to specify a column type, unless you want it to be a factor or string. Also you can query only a portion of your data if you need so thus making subset() unnecessary.