Rename all columns with characters [duplicate] - r

This question already has answers here:
Add a prefix to column names
(4 answers)
Closed 3 years ago.
I need to rename all columns in my data.frame. Right now, they are numbered 1-150 (without the X) but I would like to add "id" before each number.
Right now:
c = data.frame(1, 2)
names(c)[1] <- "1"
names(c)[2] <- "2"
What I want: so that it is id1, id2 as each column name.
How can I do this?

You can use dplyr::rename_all()
library(dplyr)
iris %>%
rename_all(~ paste0("id_", .x)) %>%
names()
or with base R
setNames(
iris,
nm = paste0(
"id_", names(iris)
)
) %>% names()

Related

Removing tab space from a column in R [duplicate]

This question already has answers here:
How to remove all whitespace from a string?
(9 answers)
Closed last year.
Can you please help me to remove a space from the ID column in my data?
I have data frame like this:
df<-data.frame(
"ID" = c("G 249485", "L 938495", "N 234987"), type=c("a","b","c"))
or
library(dplyr)
df %>%
dplyr::mutate(ID = stringr::str_remove(ID, pattern = "\\s"))
ID type
1 G249485 a
2 L938495 b
3 N234987 c
library(stringr)
df$ID <- str_replace(df$ID, " ", "")
df
library(dplyr)
df <- df %>% mutate(ID = gsub(" ","",ID))

arrange data based on user defined variables order? [duplicate]

This question already has answers here:
Order data frame rows according to vector with specific order
(6 answers)
Closed 1 year ago.
I have the following data.frame and would like to change the order of the rows in such a way that rows with variable == "C" come at the top followed by rows with "A" and then those with "B".
library(tidyverse)
set.seed(123)
D1 <- data.frame(Serial = 1:10, A= runif(10,1,5),
B = runif(10,3,6),
C = runif(10,2,5)) %>%
pivot_longer(-Serial, names_to = "variables", values_to = "Value" ) %>%
arrange(-desc(variables))
D1 %>%
mutate(variables = ordered(variables, c('C', 'A', 'B'))) %>%
arrange(variables)
Perhaps I did not get the question. If you want C then A then B, you could do:
D1 %>%
arrange(Serial, variables)
#Onyambu's answer is probably the most "tidyverse-ish" way to do it, but another is:
D1[order(match(D1$variables,c("C","A","B"))),]
or
D1 %>% slice(order(match(variables,c("C","A","B"))))
or
D1 %>% slice(variables %>% match(c("C","A","B")) %>% order())

List of unique characters in a column [duplicate]

This question already has answers here:
keep only unique elements in string in r
(2 answers)
Closed 2 years ago.
I am trying to figure out how to extract all the unique characters from a certain column. For example, if one of my column has the following rows,
june
july&
august%
then I would like r to give me the list of all the unique characters, i.e,
junely&agst%
How can this be done in R?
Split the column values at each character and paste only unique characters.
x <- c('june', 'july&', 'august%')
paste0(unique(unlist(strsplit(x, ''))), collapse = "")
#[1] "junely&agst%"
May be a Tidy approach will be useful:
library(dplyr)
library(purrr)
library(stringr)
# input
x <- c("june", "july&", "august%")
expected <- "junely&agst%"
# modify
actual <- x %>% str_split(pattern = "") %>% flatten_chr %>% unique %>% paste0(collapse = "")
# validate
stopifnot(actual == expected)

Remove a pattern in a string and mutate these values to a new column [duplicate]

This question already has answers here:
Replace specific characters within strings
(7 answers)
Closed 3 years ago.
Let's say I have this data frame:
df <- as.data.frame(c("77111","77039","5005","4032"))
and I want to create a new column where if the values start with "77", then remove the "77" and extract the remaining numbers. Otherwise, keep the values as is so that the new column looks like this:
df <- df %>% mutate(new_numbers =c("111","039","5005","4032"))
We can use str_remove to remove the 77 from the start (^) of the column
library(dplyr)
library(stringr)
df <- df %>%
mutate(col = str_remove(col, "^77"))
data
df <- data.frame(col= c("77111","77039","5005","4032"))
Another...
df <- df %>%
mutate(new_numbers = gsub('^77', '', original_column))
For an approach in base R, just use gsub:
df$new <- gsub(pattern = "^77",
replacement = "",
string = df[,1])

use dplyr to get values of a column [duplicate]

This question already has answers here:
Extract a dplyr tbl column as a vector
(8 answers)
Closed 7 years ago.
I'd like to have dplyr return a character vector instead of a data frame. Is there an easy way to do this?
#example data frame
df <- data.frame( x=c('a','b','c','d','e','f','g','h'),
y=c('a','a','b','b','c','c','d','d'),
z=c('a','a','a','a','a','a','d','d'),
stringsAsFactors = FALSE)
#desired output
unique(df$z)
[1] "a" "d"
#dplys's output
df %>%
select(z) %>%
unique()
z
1 a
7 d
Try
library(dplyr)
df %>%
select(z) %>%
unique() %>%
.$z
#[1] "a" "d"
Or using magrittr
library(magrittr)
df %>%
select(z) %>%
unique() %>%
use_series(z)
#[1] "a" "d"

Resources