add value to multiple columns in R - 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

Related

How to skip empty rows while reading multiple tabs in R?

I am trying to read an excel file with multiple tabs. For that, I use the code provided here.
The problem is that each tab has a different number of empty rows before the actual data begins. For example, the first tab has two empty rows, the second tab has three empty rows, and so on.
Normally, I would use the parameter skip in the read_excel function to indicate the number of empty lines to skip. But how do I do that for multiple tabs with different numbers of rows to skip?
perhaps the easiest solution would be to read it as it is then remove rows, i.e. yourdata <- yourdata[!is.na(yourdata$columname),] ; this would work if you don't expect any NA's in a particular column, like id. If you have data gaps everywhere you can test for all NAs in multiple columns - let me know if that's what you need.

R - conditionally modify a dataframe

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

Adding several columns into a data set

I have a dataset and I want to add several columns into the dataset in just one step. I was wondering how I can do that (such as dplyr package). For example
R<-seq(1:5)
B<-c(15,16,17,18,19)
f<-c(20,21,22,23,24)
S<-data.frame(R,B,f)
Now I want to add several columns to the data set such as
g=B/f
h=round(B/f*f,3)
d=B+f/f

Changing hundreds of column names simultaneously in R

I have a data frame with hundreds of columns whose names I want to change. I'm very new to R, so it's rather easy to think through the logic of this, but I simply can't find a relevant example online.
The closest I could sort of get was this:
projectFileAllCombinedNames <- for (i in 1:200){names(projectFileAllCombined)[i+1] <-variableNames[i]}
Basically, starting at the second column of projectFileAllCombined, I want to loop through the columns in the dataframe and assign them the data values in the second data frame. I was able to change one column name manually with this code:
colnames(projectFileAllCombined)[2]<-"newColumnName"
but I can't possibly do that for hundreds of columns. I've spent multiple hours on this and can't crack it with any number of Google searches on "change multiple columns in r" or "change column names in r". The best I can find online is examples where people change a few columns with a c() function and I get how that works, but that still seems to require typing out all the column names as parameters to the function, unless there is a way to just pass the "variableNames" file into that c() function, but I don't know of one.
Will
colnames(projectFileAllCombined)[-1] <- variableNames
not suffice?
This assumes the ordering of columns in projectFileAllCombined is the same as the ordering of the new variable names in variableNames, and that
length(variableNames) == (ncol(projectFileAllCombined) - 1)
The key point here is that the replacement function 'colnames<-'() is vectorised and can replace any number of column names in a single call if passed a vector of replacement values.

Adding based on column name not number

I am adding together columns with the following code:
cbind(rowSums(data[,c(5,10,11,15)],na.rm=TRUE),rowSums(data[,c(3,6,7)],na.rm=TRUE))
But I want to add the columns based upon the column name not their number to eliminate confusion....
Is there an easy way to do this?
Yep, just replace the numbers with the column names in quotes:
cbind(rowSums(data[,c('elephant','giraffe')],na.rm=TRUE),
rowSums(data[,c('fish','penguin','albatross')],na.rm=TRUE))
I'm collecting data on animal species today. Not sure why I'm adding them together.

Resources