in my school project, im having trouble renaming collumns

library(tidyverse)

library(psyntur)
library(janitor)
library(lavaan)
library(lavaanPlot)

#cleaning it
wellbeing0 <- waltorbrian_ %>% clean_names

#glimpse
glimpse(wellbeing0)

glimpse(wellbeing0)
Rows: 56
Columns: 10
participant_private_id <dbl> N… multiple_choice_grid_object_2_i_feel_happy_here_quantised "…
multiple_choice_grid_object_2_i_feel_calm_here_quantised <chr> "… multiple_choice_grid_object_2_i_feel_frustrated_here_quantised "…
multiple_choice_grid_object_2_i_feel_anxious_here_quantised <chr> "… multiple_choice_grid_object_2_i_feel_uneasy_here_quantised "…
multiple_choice_grid_object_2_i_feel_i_belong_here_quantised <chr> "… multiple_choice_grid_object_2_i_feel_safe_here_quantised "…
multiple_choice_grid_object_2_i_feel_lonely_here_quantised <chr> "… multiple_choice_grid_object_2_i_feel_like_being_active_or_exercising_here_quantised "…

renaming

wellbeing00 <- wellbeing0 %>%
rename(id = participant_private_id,
WB1 = multiple_choice_grid_object_2_i_feel_happy_here_quantised,
WB2 = multiple_choice_grid_object_2_i_feel_calm_here_quantised,
WB3 = multiple_choice_grid_object_2_i_feel_frustrated_here_quantised,
WB4 = multiple_choice_grid_object_2_i_feel_anxious_here_quantised,
WB5 = multiple_choice_grid_object_2_i_feel_uneasy_here_quantised,
WB6 = multiple_choice_grid_object_2_i_feel_i_belong_here_quantised,
WB7 = multiple_choice_grid_object_2_i_feel_safe_here_quantised,
WB8 = multiple_choice_grid_object_2_i_feel_lonely_here_quantised,
WB9 = multiple_choice_grid_object_2_i_feel_like_being_active_or_exercising_here_quantised)

The code looks correct if unwieldy. Do you have an error message? What makes you think it's not working?

Maybe, can you simplify and rename only the first column?

the error message is this;

Error in rename(id = participant_private_id, WB1 = multiple_choice_grid_object_2_i_feel_happy_here_quantised, :
unused arguments (id = participant_private_id, WB1 = multiple_choice_grid_object_2_i_feel_happy_here_quantised, WB2 = multiple_choice_grid_object_2_i_feel_calm_here_quantised, WB3 = multiple_choice_grid_object_2_i_feel_frustrated_here_quantised, WB4 = multiple_choice_grid_object_2_i_feel_anxious_here_quantised, WB5 = multiple_choice_grid_object_2_i_feel_uneasy_here_quantised, WB6 = multiple_choice_grid_object_2_i_feel_i_belong_here_quantised, WB7 = multiple_choice_grid_object_2_i_feel_safe_here_quantised, WB8 = multiple_choice_grid_object_2_i_feel_lonely_here_quantised, WB9 = multiple_choice_grid_object_2_i_feel_like_being_active_or_exercising_here_quantised)

It just isn't working for some reason and pops up with that. I have also tried to just rename one column, but that hasn't worked.

Hi @kukus , thank you for your question.

I wonder if the dplyr::rename() function is being masked by another function of the same name in your environment, such as plyr::rename().

Could you try dplyr::rename()and let us know what happens?

wellbeing00 <- wellbeing0 %>%
  dplyr::rename(
    id = participant_private_id,
    WB1 = multiple_choice_grid_object_2_i_feel_happy_here_quantised,
    WB2 = multiple_choice_grid_object_2_i_feel_calm_here_quantised
    # ... etc
  )
1 Like

Hi I just tried it, it came up with this:
Error in dplyr::rename():
! Can't rename columns that don't exist.
:heavy_multiplication_x: Column Multiple Choice Grid object-2 I feel happy here\n Quantised doesn't exist.
Run rlang::last_trace() to see where the error occurred.

Hi @kukus ,

This looks like progress :slight_smile:

I assumed you've first run wellbeing0 through janitor's clean_names() function before attempting to rename:

However, the feedback you're getting is that it can't find a column that doesn't use a 'clean name':

Could you try again with the clean_names and see what that gives?

wellbeing00 <- wellbeing0 %>%
  janitor::clean_names() |> 
  dplyr::rename(
    id = participant_private_id,
    WB1 = multiple_choice_grid_object_2_i_feel_happy_here_quantised,
    WB2 = multiple_choice_grid_object_2_i_feel_calm_here_quantised
    # ... etc
  )

Hi I just tried, it comes up with an error message again:
Error in UseMethod("rename") :
no applicable method for 'rename' applied to an object of class "list"

Thanks, @kukus . Could you copy/paste the code you're using into this thread, please? It might be useful to help spot if there is something we've missed.

Yes, here it is:

packages

library(tidyverse)
library(psyntur)
library(janitor)
library(lavaan)
library(lavaanPlot)

glimpse data

glimpse(darell_real)
glimpse(waltorbrian_)
glimpse(susan)
glimpse(gerald)

green <- gerald %>% clean_names
socialcohesion <- susan %>% clean_names
wellbeing <- waltor_brian %>% clean_names
demographics <- darell_real %>% clean_names

#selecting specific demographics
demographics1_selected <- demographics %>%
select(participant_private_id, text_input_object_3_value,
dropdown_object_4_response, ends_with("quantised"))

change name of each question to (variable)Q(number)

#rename variables to make it easier and so that it is presnted on results right

use eg. green_cleaned$WQ1 <- as.numeric(green_space_cleaned$WQ1_) for everyone

such as WQ2, WQ3, WQ4 etc.

you have to select and rename all scales questions simply to WQ1, or 2 etc.

how you rename depends on what is easier

use rename(id = partiicpant_private_id), and rename questions by

(question name) = (old question name)

#before going ahead, there is a huge anomaly in row 45, a person aged 2003

this needs to be removed as this was a tester

demographics2_selected <- demographics1_selected[-c(45),]

#starting on demographics

cleaning the data questions

demographics_cleaned1 <- demographics2_selected %>%
rename(id = participant_private_id,
Age = text_input_object_3_value,
Gender = dropdown_object_4_response,
dem3 = dropdown_object_4_quantised,
Time = multiple_choice_object_5_quantised)

remove the duplicated data set from demographics

demographics_cleaned2 <- demographics_cleaned1 %>%
select(id, Age, Gender, Time)

i have successfully done this

i have removed the 2003 year old from gerlad

this was done using excel

i have also made it all quantised

the file name is GERALD_

rename the questions in gerald

Greenspaces_ <- GERALD_ %>%
rename(id = Participant Private ID,
GS1 = Multiple Choice Grid object-2 There should be more green areas in the neighbourhood Quantised,
GS2 = Multiple Choice Grid object-2 To go to the park it is necessary to go to other areas of the city Quantised,
GS3 = Multiple Choice Grid object-2 By increasing the number of homes, green areas and have almost completely disappeared Quantised,
GS4 = Multiple Choice Grid object-2 In the neighbourhood there is a garden/park where people can meet Quantised,
GS5 = Multiple Choice Grid object-2 There are few trees in the neigbourhood Quantised,
GS6 = Multiple Choice Grid object-2 There would be green areas but for most part they are unusable or closed to the public Quantised,
GS7 = Multiple Choice Grid object-2 The green spaces in the neighbourhood cannot be frequented because they are poorly kept Quantised,
GS8 = Multiple Choice Grid object-2 The green areas in the neighbourhood are in good condition Quantised,
GS9 = Multiple Choice Grid object-2 The usable green areas in the neighbourhood are only the private ones because the public ones are too poorly kept Quantised
)

i have done work on the well being scale on excel

i have removed the non-numeric responses

i have also deleted the 2003 year old's data

the new data set is called waltorbrian_

#additional libraries
library(dplyr)
library(plyr)

glimpse

glimpse(wellbeing0)

library(dplyr)
lookup <- c(wellbeing0, new = "unknown")

#viewing
view(wellbeing0)

try(rename(wellbeing0, all_of(lookup)))

#renaming
wellbeing00 <- wellbeing0 %>%
dplyr::rename(WB1 = Multiple Choice Grid object-2 I feel happy here Quantised,
WB2 = Multiple Choice Grid object-2 I feel calm here Quantised,
WB3 = Multiple Choice Grid object-2 I feel frustrated here Quantised,
WB4 = Multiple Choice Grid object-2 I feel anxious here Quantised,
WB5 = Multiple Choice Grid object-2 I feel uneasy here Quantised,
WB6 = Multiple Choice Grid object-2 I feel I belong here Quantised,
WB7 = Multiple Choice Grid object-2 I feel safe here Quantised,
WB8 = Multiple Choice Grid object-2 I feel lonely here Quantised,
WB9 = Multiple Choice Grid object-2 I feel like being active or exercising here Quantised)

view(wellbeing0)

wellbeing00 <- wellbeing0 %>%
janitor::clean_names() |>
dplyr::rename(WB1 = Multiple Choice Grid object-2 I feel happy here Quantised,
WB2 = Multiple Choice Grid object-2 I feel frustrated here Quantised,
WB3 = Multiple Choice Grid object-2 I feel anxious here Quantised,
WB4 = Multiple Choice Grid object-2 I feel uneasy here Quantised,
WB5 = Multiple Choice Grid object-2 I feel I belong here Quantised,
WB6 = Multiple Choice Grid object-2 I feel safe here Quantised,
WB7 = Multiple Choice Grid object-2 I feel lonely here Quantised,
WB8 = Multiple Choice Grid object-2 I feel like being active or exercising here Quantised,
WB9 = Multiple Choice Grid object-2 I feel calm here Quantised)

Just to add, I have evidently tried a lot of different things, as I was looking at other threads as I was confused, although I dont know if that changed anything

Hi @kukus ,

You're on the right lines. I can see you've tried lots of things with this and I think you're nearly there.

The janitor::clean_names() function changes your variable names to be more computer-friendly. So a variable that is named:
Multiple Choice Grid object-2 I feel happy here Quantised becomes named multiple_choice_grid_object_2_i_feel_happy_here_quantised.

In step below you run wellbeing0 through janitor::clean_names() so the variables should all be named in lower-case and underscored, whereas you're asking dplyr::rename() to check for the old variable name with capital letters and spaces:

Let's try the following:

wellbeing00 <- wellbeing0 |>
  janitor::clean_names() |>
  dplyr::select(
    WB1 = multiple_choice_grid_object_2_i_feel_happy_here_quantised
  )

You can rename variables in the dplyr::select function, so we use it here to select just one variable (to keep things simple until we sort out what is happening).

Notice how I've used the 'clean' version of the variable name.

What do you get if you run this snippet?

I got this
Error in dplyr::select():
! select() doesn't handle lists.
Run rlang::last_trace() to see where the error occurred

I am assuming maybe there is something wrong with the format of my data?

Hmm, is it possible for you to share a small sample of the data you're working with?

The following will output the first 5 rows of data in wellbeing0:

dput(head(wellbeing0, 5))

If there are no sensitive data in this, could you copy and paste this to this thread?

Yes of course here it is
list(participant_private_id = c(NA, 11861371, 11861362, 11861421,
11861469, 11861477, 11861510, 11861603, 11861630, 11861738, 11861753,
11861805, 11861832, 11861835, 11862021, 11862321, 11862540, 11862711,
11862782, 11863077, 11863113, 11863223, 11863167, 11863555, 11865107,
11865115, 11866353, 11866581, 11868926, 11870125, 11875967, 11877553,
11877625, 11877706, 11888964, 11905218, 11908275, 11908330, 11908501,
11909041, 11909062, 11909158, 11911586, 11912127, 11912386, 11912528,
11913823, 11913876, 11913878, 11913992, 11914103, 11914197, 11914344,
11914349, 11914541, 11914603), multiple_choice_grid_object_2_i_feel_happy_here_quantised = c("Please review the below questions and provide an answer.",
"2", "4", "3", "3", "3", "4", "4", "3", "4", "4", "3", "4", "4",
"3", "4", "4", "3", "3", "4", "5", "4", "4", "3", "3", "1", "4",
"4", "2", "3", "3", "4", "4", "4", "3", "4", "5", "4", "5", "4",
"3", "4", "3", "5", "3", "4", "4", "4", "4", "3", "2", "3", "4",
"3", "3", "4"), multiple_choice_grid_object_2_i_feel_calm_here_quantised = c("Please review the below questions and provide an answer.",
"4", "3", "3", "3", "3", "3", "4", "3", "4", "4", "2", "3", "4",
"3", "4", "4", "4", "3", "4", "5", "3", "4", "2", "2", "1", "4",
"4", "3", "3", "3", "4", "3", "4", "4", "5", "5", "4", "5", "4",
"3", "5", "3", "5", "3", "5", "2", "4", "4", "3", "3", "3", "5",
"3", "4", "4"), multiple_choice_grid_object_2_i_feel_frustrated_here_quantised = c("Please review the below questions and provide an answer.",
"5", "2", "3", "3", "2", "2", "3", "4", "2", "4", "4", "4", "1",
"2", "2", "2", "4", "3", "2", "1", "2", "2", "4", "2", "3", "1",
"3", "3", "4", "5", "2", "2", "2", "2", "2", "2", "2", "1", "2",
"4", "1", "2", "1", "3", "2", "4", "3", "2", "2", "3", "3", "1",
"4", "2", "2"), multiple_choice_grid_object_2_i_feel_anxious_here_quantised = c("Please review the below questions and provide an answer.",
"4", "4", "4", "3", "4", "4", "2", "4", "2", "4", "4", "3", "1",
"4", "2", "3", "2", "3", "2", "1", "4", "2", "4", "3", "5", "2",
"4", "2", "4", "4", "2", "3", "3", "4", "2", "2", "1", "2", "2",
"4", "1", "4", "1", "3", "2", "4", "4", "4", "4", "4", "5", "1",
"5", "2", "3"))

Ah, perfect.

How does this work for you?

wellbeing00 <- wellbeing0 |> 
  dplyr::as_tibble() |> 
  janitor::clean_names() |> 
  dplyr::rename(
    id = participant_private_id,
    WB1 = multiple_choice_grid_object_2_i_feel_happy_here_quantised,
    # etc
  )

Yes! It worked. The whole thing worked.
Thank you so much you have been so helpful.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.