Select last row and place it on the top [duplicate] - r

This question already has answers here:
adding last value to the top of the data frame.
(4 answers)
Closed 3 years ago.
I have a data frame and I'd like to reorder it. I'd like to make the last row the top row.
Example, if I type mtcars into the console the last car listed is a volvo 142E. Suppose I wanted to make this the first row, how would I do that?
dplyr/tidyverse or base r preferred.

In base R -
mtcars[c(nrow(mtcars), seq(nrow(mtcars)-1)), ]
# top 6 rows
mpg cyl disp hp drat wt qsec vs am gear carb
Volvo 142E 21.4 4 121 109 4.11 2.780 18.60 1 1 4 2
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Here's a generalized function for moving any row to top -
move_to_top <- function(df, n) {
df[c(n, setdiff(1:nrow(df), n)), ]
}
head(move_to_top(mtcars, 32))
mpg cyl disp hp drat wt qsec vs am gear carb
Volvo 142E 21.4 4 121 109 4.11 2.780 18.60 1 1 4 2
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2

Here's a base R method which also works for rows other than the last row
to_top <- nrow(mtcars)
mtcars[order(seq(nrow(mtcars)) != to_top),]
# mpg cyl disp hp drat wt qsec vs am gear carb
# Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
# ...
to_top <- which(rownames(mtcars) == 'Valiant')
mtcars[order(seq(nrow(mtcars)) != to_top),]
# mpg cyl disp hp drat wt qsec vs am gear carb
# Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
# Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
# Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
# Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
# ...
You can also use setdiff for the same result
mtcars[c(to_top, setdiff(seq(nrow(mtcars)), to_top)),]
Or the order method in dplyr
library(dplyr)
mtcars %>%
rownames_to_column() %>%
arrange(row_number() != n())
# rowname mpg cyl disp hp drat wt qsec vs am gear carb
# 1 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# 2 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
# 3 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
# 4 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
# 5 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
# 6 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
# ...

Another idea is to subset and bind rowwise, i.e.
rbind(tail(mtcars, 1), head(mtcars, -1))
# mpg cyl disp hp drat wt qsec vs am gear carb
#Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
#Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#...

We can use slice
library(tidyverse)
mtcars%>%
rownames_to_column('rn') %>%
slice(c(n(), 1:(n()-1))) %>%
column_to_rownames('rn')
# mpg cyl disp hp drat wt qsec vs am gear carb NA_1 NA_2
#Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 NA 21.4
#Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 NA 21.0
#Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 NA 21.0
#Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 NA 22.8
#Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 NA 21.4
# ...

Related

If column A equals criteria return value of column B in column C

Using the R inbuilt dataset
mtcars
I want to make a column called "want".
mtcars$want<-NA
When column "carb" is equal to 1 (Column A), input value of column "qsec" (Column B) in column "want" (Column C).
If carb is not equal to 1 do nothing.
The first 5 rows of the new dataset should look like this:
mpg cyl disp hp drat wt qsec vs am gear carb want
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 NA
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 NA
Datsun 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 18.61
Hornet Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 19.44
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 NA
This should do the job:
mtcars$want <- ifelse(mtcars$carb == 1, mtcars$qsec, NA)
head(mtcars, 5)
mpg cyl disp hp drat wt qsec vs am gear carb want
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 NA
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 NA
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 18.61
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 19.44
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 NA
If you only want to achieve it in the print out you could try the following (in the data.frame itself this will still be shown as NA):
mtcars$want <- ifelse(mtcars$carb == 1, mtcars$qsec, "")
head(mtcars, 5)
mpg cyl disp hp drat wt qsec vs am gear carb want
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 18.61
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 19.44
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
If it is helpful, I am of the impression that a loop over the columns should work. One can modify the loop or add further conditionals as appropriate to fill in the other values of the column.
#written in R version 4.2.1
data(mtcars)
mtcars$want = 0
for(i in 1:dim(mtcars)[1]){
if(mtcars$carb[i] == 1){
mtcars$want[i] = mtcars$qsec[i]
}}
Result:
head(mtcars)
# mpg cyl disp hp drat wt qsec vs am gear carb want
#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 0.00
#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 0.00
#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 18.61
#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 19.44
#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 0.00
#Valiant
What you can do is first set a value to your new column "want" for example 2. You can use ifelse to do your criteria and return "want" if do nothing like this:
mtcars$want <- 2
library(dplyr)
mtcars %>%
mutate(want = ifelse(carb == 1, qsec, want)) %>%
head(5)
#> mpg cyl disp hp drat wt qsec vs am gear carb want
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 2.00
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 2.00
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 18.61
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 19.44
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 2.00
Created on 2022-06-30 by the reprex package (v2.0.1)

Get column name of the nearest or matching value in a column

head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
I want two new columns.
near_wt which has the value same or near to the wt column.
I got this using
mtcars$near_wt <- mtcars[, -col][cbind(1:nrow(mtcars),
max.col(-abs(mtcars[, col] - mtcars[, -col])))]
This works. The next thing I need is the column name from which each value of "near_wt" comes.
Does col is 6? If so, you may try this way. For convenience, I define x as
x <- cbind(1:nrow(mtcars),max.col(-abs(mtcars[, col] - mtcars[, -col])))
Then,
mtcars$near_wt <- mtcars[, -6][x]
mtcars$near_wt_name <- names(mtcars[-6])[x[,2]]
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb near_wt near_wt_name
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 3.90 drat
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 3.90 drat
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 1.00 am
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 3.08 drat
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 3.15 drat
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 3.00 gear

Rename only if field exists, otherwise ignore

One can rename a field:
mtcars %>% rename(bla = mpg)
But if the field doesn't exist, an error:
mtcars %>% rename(MPG = mpg, CYL = cyl, bla = uyhgfrtgf)
Error: Can't rename columns that don't exist.
x Column `uyhgfrtgf` doesn't exist
I looked at ?rename_if and it says this is now superseded by rename_with().
What's the 'right' way to attempt to rename fields but with a possibility they don't exist (e.g. in this case a ShinyApp with filter selectors).
Use any_of() with a named vector. any_of() doesn't error if a variable isn't found.
library(dplyr)
lookup <- c(MPG = "mpg", CYL = "cyl", bla = "uyhgfrtgf")
mtcars %>%
rename(any_of(lookup))
MPG CYL disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
...
I'm pretty sure you can do this with NSE but I can't handle this (yet?).
So you could define a lookup variable first:
lookup <- c("mpg" = "MPG", "cyl" = "CYL", "uyhgfrtgf" = "bla")
Attention: it's the other way round as in your rename function.
Now you could use
library(dplyr)
df %>%
rename_with(.fn = ~lookup[.x], .cols = intersect(names(.), names(lookup)))
which returns
MPG CYL disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
We may use
library(dplyr)
lookup <-c("MPG" = "mpg", "CYL" = "cyl", "bla" = "uyhgfrtgf")
mtcars %>%
rename(!!! lookup[names(.) %in% lookup])
MPG CYL disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
...
We could use rename_with from dplyr package and wrap it in a ~case_when function:
library(dplyr)
mtcars %>%
rename_with(
~ case_when(
. == "mpg" ~ "MPG",
. == "cyl" ~ "CYL",
. == "bla" ~ "uyhgfrtgf",
TRUE ~ .))
MPG CYL disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
You could use the following solution:
library(dplyr)
library(stringr)
names <- c("mpg", "cyl", "sdad")
reps <- c("MPG", "CYL", "BLA")
mtcars %>%
rename_with(~ str_replace_all(.x, "\\w+", reps[names(mtcars) %in% names]), matches(names))
MPG CYL disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

how to swap the third column with the last column, and then delete the swapped last column in R

i want to swap a specific column with the last column, and then delete the last column after swapping. After delete ncol(testFrame) will decrease by 1
Usually a reproducible example is expected but your description is clear enough to understand what you want to do.
Using mtcars as sample data
df <- mtcars
head(df)
# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
swap_column <- 3
cols <- seq_len(ncol(df))
df1 <- df[replace(cols, cols == swap_column, ncol(df))][-ncol(df)]
head(df1)
# mpg cyl carb hp drat wt qsec vs am gear
#Mazda RX4 21.0 6 4 110 3.90 2.620 16.46 0 1 4
#Mazda RX4 Wag 21.0 6 4 110 3.90 2.875 17.02 0 1 4
#Datsun 710 22.8 4 1 93 3.85 2.320 18.61 1 1 4
#Hornet 4 Drive 21.4 6 1 110 3.08 3.215 19.44 1 0 3
#Hornet Sportabout 18.7 8 2 175 3.15 3.440 17.02 0 0 3
#Valiant 18.1 6 1 105 2.76 3.460 20.22 1 0 3
We replace the column number swap_column with last column number (ncol(df)) and then remove the last column (-ncol(df)).
We can do this conveniently with add_column from tibble. The .after and .before parameters can take either column index or column name. Suppose, we need to shift last column to third position
library(tibble)
data(mtcars)
df1 <- add_column(mtcars[-ncol(mtcars)], mtcars[ncol(mtcars)], .after = 2)
head(df1)
# mpg cyl carb disp hp drat wt qsec vs am gear
#Mazda RX4 21.0 6 4 160 110 3.90 2.620 16.46 0 1 4
#Mazda RX4 Wag 21.0 6 4 160 110 3.90 2.875 17.02 0 1 4
#Datsun 710 22.8 4 1 108 93 3.85 2.320 18.61 1 1 4
#Hornet 4 Drive 21.4 6 1 258 110 3.08 3.215 19.44 1 0 3
#Hornet Sportabout 18.7 8 2 360 175 3.15 3.440 17.02 0 0 3
#Valiant 18.1 6 1 225 105 2.76 3.460 20.22 1 0 3

How to insert a new column to a data frame with uniform values

I have the following data frame:
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
What I want to do is to insert new columns called 'new_column' with values 'foo'
resulting in this:
new_column mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 foo 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag foo 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 foo 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive foo 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout foo 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant foo 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
I tried this but failed:
library(zoo)
zoo("foo",mtcars$new_columns)
What's the right way to do it?
You can just use cbind (if the position of the column must be first):
head(cbind("new_column" = "foo", mtcars))
# new_column mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 foo 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag foo 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 foo 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive foo 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout foo 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# Valiant foo 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
If the column can be at the end, you can also do:
mtcars$new_column <- "foo"

Resources