R - conditionally modify a dataframe - r

I want to check to see if a dataframe has a certain number of columns, and then conditionally modify the dataframe based on the number of columns it has.
If I try
ifelse(ncol(Table1)=="desired number of columns",Table1[,ColumnsSelected],Table1[,ColumnsSelected2]))
I get Table 1 back, but only one column, and it looks weird. Is there a way that I can change this to make it so that I can return a dataframe with the desired columns based on the number of columns in the dataframe?
I have roughly 700 tables that have 2 types of formatting that I would prefer not to have to individually scan to reformat.
Please advise

Related

Is there a way to use the value of a cell in R in subsequent functions?

I have a dataframe of timestamps, and want to edit multiple CSVs according to the timestamp listed in the dataframe (cutting out irrelevant data)
Is there a way for me to use the value of a cell in a dataframe in subsequent functions? I have about 1200 files to edit.
Most answers recommend using coordinates to extract a specific cell value but I feel there must be a more efficient way of doing this.

How to shift rows down based on value in column R?

I have a data frame that looks kind of like this...but much larger
I want to look at the record_id column and shift the right side columns down when the row says admin_time. Then make that previous row NA. Then when I write it to a csv, I'll just use the na = "" to make those cells blank
For example, in the first few rows, it would look like this...
No need to try to recreate the data frame. I was thinking maybe a for loop would work with an embedded if statement to review the patient_id, record_id, and pk_day. I was just looking for alternate suggestions or how to use a statement within the loop to pick out the admin line and do what I mentioned above

How to filter out ALL rows of a table with just one column duplicate values in R?

I'm looking for help with a problem I'm trying to solve in R.
I have DNA alignment results, a big dataset (more than 200 000 rows, and 20 columns) and I want to clean it and delete non-specific sequence and have at the end just one DNA sequence name for one species.
I've tried unique,duplicate and distinct function but they always keep the first duplicate rows and I don't want them, I would like to delete ALL the duplicate rows.
Do you have an idea how to solve my problem?

add value to multiple columns in R

I want to update my dataframe adding value in various columns.
My dataframe has A-Z columns name.
I created a list specifiying which column want to update.
upd<-c("a","b","c","d")
And i used simple R syntax to update
df<-df[,names(df)%in%upd]+5
But I only have a,b,c,d columns.
is there one way to save all columns?.
thanks

R naming convention/tricks for many columns in data.table / data.frame

I have a list of, say, n=10 data.tables (or data.frames).
Performing names(myList) returns the unique table names.
Performing names(myList[[i]]) (for i in 1:n) returns identical output for each value of i - i.e. each data.table has identical column names.
I need to merge all the data tables into one large data table, but would like to preserve the name of the list data.table for each column somehow, in order to keep an overview of where each column originated from.
Is there a trick to doing this, such as giving the columns keys? Or must one just prepend the table name to each of the columns in the final result? This would make the names pretty long in my case.
I want to avoid having to remember (or think about) which columns belongs to which table. Just for comparisons sake, I'd like to run str(myBigTable) or summary(myBigTable) and see something like Excel shows here [but vertically displayed in R]:

Resources