Loop through each column and subtract another column in R [duplicate] - r

This question already has answers here:
Subtract a column in a dataframe from many columns in R
(3 answers)
Closed 5 years ago.
Let's say I have a data frame with values from column 1-11. For columns 1-10, I want to subtract column 11. Basically I want dataframe[1]-dataframe[11], dataframe[2]-dataframe[11], dataframe[3]-dataframe[11], dataframe[n] - dataframe[11], etc...
So basically, in VBA terms, it would be like:
For each column in dataframe, starting with column 1, subtract column 11 from it.
How do I do that?
Thanks!

You can just do the subtraction directly.
df[,1:10] = df[,1:10] - df[,11]

Related

sort dataframe r by column values [duplicate]

This question already has answers here:
Order dataframe in R [duplicate]
(1 answer)
Sort (order) data frame rows by multiple columns
(19 answers)
Closed last year.
I would like to reorder this dataframe based on the values ​​of the "new" column, how can I do? tell me the exact formula, thanks
Substituting "DF" as your data frame, you can use this line of code:
DF[order(DF$new), ]
If you just wanted to view the data frame sorted, you can use
View(DF)
And click the arrows on the column name, which automatically sorts the data frame by ascending/descending.

Automate data frame width in R [duplicate]

This question already has answers here:
how to remove multiple columns in r dataframe?
(8 answers)
Select column 2 to last column in R
(4 answers)
Closed 2 years ago.
I have a data frame that I import from excel each week with 40+ columns. Each week the data frame has a different number of columns, I am only interested in the first 40. I take the data frame, drop the columns after 40 and rbind to another data frame (that has 40 columns).
The code I use to drop the columns is"
df = df[ -c(40:45) ] #assume df has 45 columns this week.
I would like to find a step to automate the lendth of columns to drop, similar to length(df$x) type of idea. I try width(df) but doesn't seem to work?
Any ideas please?

How to delete specific columns in R [duplicate]

This question already has an answer here:
How do I delete columns in a dataframe if the name begins with X? [duplicate]
(1 answer)
Closed 3 years ago.
I have a data frame containing 2000 columns. Majority of the columns have "X111, X222 ,X123" and I want remove columns that starts with the name X
df[,-grep("^X",names(df)]
Grep logic looks for words starting (^) with X.

How to recalculate columns and store new values in a same dataframe? [duplicate]

This question already has answers here:
How can I divide one column of a data frame through another?
(2 answers)
Closed 3 years ago.
The below link shows my initial data frame.
The inc_per column has wrong values in it. so I want to recalculate this and update data frame with new values in it. So I tried this code:
cars<- within(cars,cars$per_inc<- (cars$oldprice / cars$newprice)*100)
But it created unnecessary columns and I didn't get my desired output
My desired output can be seen below:
it is very easy use
cars$per_inc <- (cars$oldprice / cars$newprice)*100
this will replace the current column values with new one.

Subsetting an R Matrix [duplicate]

This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 4 years ago.
in R programming, how do I subset a matrix so that I can skip columns in between? I only know how to do it continuously such as 1:4, but what if I want the first, second, and fourth colum
You can select specific columns as follows:
new_df <- x[,c(1,2,4)]# Select column 1,2 and 4

Resources