How to delete row in for loop function? - r

I want to delete the second row in every sheet in an excel file. Actually, i have done this.
for (i in 1:5)
{bulan=read_excel(file, sheet=sheet[i],skip = 2)}
but it deletes the first 2 rows. How to only delete the second one? Thanx

It depends whether you would really like to delete a row in your excel sheet or read an excel sheet without a certain row.
Because what your are doing here is actually not deleting rows, but skipping the first two rows when reading the sheets. The function read_excel returns a dataframe.
If you want the dataframe without the second row, what you could do is:
for(i in 1:5){bulan <- read_excel(file, sheet=sheet[i])
bulan <- bulan[-2,]
}
However, this would not make much sense as is, since bulan gets overwritten in every step of the for loop.
If you would like to delete rows in your excel file using R, you could read the file, delete the corresponding row of the dataframe in R, and read the dataframe to an excel file again. Apparently there is am R package called "xlsx" for writing excel files.

Related

RStudio - weird formatting to append new row to .csv file

I am trying to append a new row of data to an existing .csv file but everytime I do so, it doesn't add it to a new row. Instead, it just appends to the last row like so:
Also, the first column is supposed to show the date but I'm not sure why it shows up as hashtags. But in the column where it shows 33882020-09-24, it should end at 3388 and everything else after that should be in their respective column below.
Here is what I have for my code. I've followed multiple forums on how to append to .csv files and did exactly what was shown so I am at a loss..
Any suggestions would be greatly appreciated! Thank you in advance!!
For the data, is possible that the Rstudio don't recognise the number as a data (because you use -, and for R is a operation. Try don't use this symbol, but for example_) or try to write 24sept2020.
For add a new row a found this info:
How can a add a row to a data frame in R?

How to remove the first row in every sheet in r?

I have an excel document with multiple sheets. I was curious if there was a way to remove the first row of every sheet as there is a header file on the sheet that isn't necessary but this automatically appears on each sheet. I would rather not open the file and remove the first row as this can cause possible data errors.
"Many functions that read-in data will have a "skip" argument (under some name). E.g., for readxl::read_excel() you can use skip = 1 to skip one line. If you check out the documentation for the function you are using it should help clarify things."
- Andrew

How can I get a count of columns in multiple Excel Worksheets in R?

I am working in R, trying to combine the data from a Sheet in 250 different Excel Files into one data frame. I have put all these files in a list called "Data.Files" and they all contain a sheet named "Selected".
However, not every "Selected" sheet has the same amount of columns and I need to figure out the column count for each so we can do an RBind. I already have the files in a list "Data.Files".
I tried to do df1=read_xlsx(data.files[1], ncol(sheet="Selections"))
But it will not work. Is there a way to do this so it prints out the column count for each file in the list? I am trying to figure out a way to get the Column Count of each Names sheet in the "Data.Files" list so it would print out something like this
Df1=93
Df2=94
etc for all the frames I've pulled from files. Any help is appreciated.
E: Essentially all I would need to do is to export the Global Environment summary as a csv.

Selecting all sheet except the first in r and then bind it

I have multiple excel file each containing different number of sheet. I want to merge all the sheet of all files except the 1st sheet of each file into a new data frame.
If you want to use rbind the columns must have the same name. Ignoring the 1st sheet should be done when calling the frame [-1], a little bit more information would help.

read a selected column from multiple csv files and combine into one large file in R

Hi,
My task is to read selected columns from over 100 same formatted .csv files in a folder, and to cbind into a big large file using R. I have attached a screen shot in this question for a sample data file.
This is the code I'm using:
filenames <- list.files(path="G:\\2014-02-04")
mydata <- do.call("cbind",lapply(filenames,read.csv,skip=12))
My problem is, for each .csv file I have, the first column is the same. So using my code will create a big file with duplicate first columns... How can I create a big with just a single column A (no duplictes). And I would like to name the second column read from each .csv file using the value of cell B7, which is the specific timestamp of each .csv file.
Can someone help me on this?
Thanks.

Resources