Dear R users,
I have a dataframe that contains several variables, among which 105
correspond to scores on certain trials. Unfortunately, when I imported this
dataframe into R, I realised that the variable names corresponding to each
trial begin with digits, which violates R naming conventions.
I am trying to relabel these variables by adding a 'v' as a prefix to each
of them, I'd like to use tidyverse, but I am struggling with this process
of renaming. When I run this chunk of code, no error occurs but my
variables are not renamed. I'm fairly new to R and I can't understand what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
I appreciate if you can help.
Best,
Anne
Rename variables starting with digits
8 messages · Anne Zach, Andrew Simmons, Eric Berger +4 more
Hello, I think you have to use 'matches' instead of 'starts_with', I believe starts_with does not accept a regular expression whereas matches does.
On Tue, Oct 5, 2021 at 12:15 PM Anne Zach <anne.zach.zach at gmail.com> wrote:
Dear R users,
I have a dataframe that contains several variables, among which 105
correspond to scores on certain trials. Unfortunately, when I imported this
dataframe into R, I realised that the variable names corresponding to each
trial begin with digits, which violates R naming conventions.
I am trying to relabel these variables by adding a 'v' as a prefix to each
of them, I'd like to use tidyverse, but I am struggling with this process
of renaming. When I run this chunk of code, no error occurs but my
variables are not renamed. I'm fairly new to R and I can't understand what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
I appreciate if you can help.
Best,
Anne
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi Anne, It would be helpful to include at least part of behavioral_df for people to understand the issue better. Please do the following in R and post the output. dput( head( behavioral_df) ) Also, set your email to plain text as HTML is stripped from emails on this list. Best, Eric
On Tue, Oct 5, 2021 at 7:15 PM Anne Zach <anne.zach.zach at gmail.com> wrote:
Dear R users,
I have a dataframe that contains several variables, among which 105
correspond to scores on certain trials. Unfortunately, when I imported this
dataframe into R, I realised that the variable names corresponding to each
trial begin with digits, which violates R naming conventions.
I am trying to relabel these variables by adding a 'v' as a prefix to each
of them, I'd like to use tidyverse, but I am struggling with this process
of renaming. When I run this chunk of code, no error occurs but my
variables are not renamed. I'm fairly new to R and I can't understand what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
I appreciate if you can help.
Best,
Anne
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On 04/10/2021 2:02 p.m., Anne Zach wrote:
Dear R users,
I have a dataframe that contains several variables, among which 105
correspond to scores on certain trials. Unfortunately, when I imported this
dataframe into R, I realised that the variable names corresponding to each
trial begin with digits, which violates R naming conventions.
I am trying to relabel these variables by adding a 'v' as a prefix to each
of them, I'd like to use tidyverse, but I am struggling with this process
of renaming. When I run this chunk of code, no error occurs but my
variables are not renamed. I'm fairly new to R and I can't understand what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
You should also consider not renaming the columns. R allows non-standard names to be used as long as you quote them somehow. For example, behavioral_df[, "50%"] will get you the column with name "50%", as will behavioral_df$`50%` I suspect most tidyverse functions will be fine with the `50%` style of quoting. Duncan Murdoch
... and to add to what Eric and Duncan have said, what you have as column names depends on how the data were imported. e.g.:
d1 <-data.frame(a = 1:3, `1b` = letters[1:3]) ## check.names has a
default of TRUE
names(d1)
[1] "a" "X1b" ## note the conversion to a syntactically valid name. See ?data.frame and ?make.names for details
d2 <-data.frame(a = 1:3, `1b` = letters[1:3], check.names = FALSE) names(d2)
[1] "a" "1b" So what does names(behavioral_df) give? Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Oct 5, 2021 at 10:11 AM Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
On 04/10/2021 2:02 p.m., Anne Zach wrote:
Dear R users, I have a dataframe that contains several variables, among which 105 correspond to scores on certain trials. Unfortunately, when I imported
this
dataframe into R, I realised that the variable names corresponding to
each
trial begin with digits, which violates R naming conventions. I am trying to relabel these variables by adding a 'v' as a prefix to
each
of them, I'd like to use tidyverse, but I am struggling with this process of renaming. When I run this chunk of code, no error occurs but my variables are not renamed. I'm fairly new to R and I can't understand
what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
You should also consider not renaming the columns. R allows non-standard names to be used as long as you quote them somehow. For example, behavioral_df[, "50%"] will get you the column with name "50%", as will behavioral_df$`50%` I suspect most tidyverse functions will be fine with the `50%` style of quoting. Duncan Murdoch
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi Anne,
As mentioned above, you may have to do nothing. Here is an example
that might clarify that:
azdat<-read.table(text="subject 1 2 3
1 10 20 30
2 11 22 33",
header=TRUE,stringsAsFactors=FALSE)
azdat
subject X1 X2 X3
1 1 10 20 30
2 2 11 22 33
As you can see, R simply prepends an "X" to numeric names. If you
really want a "v":
names(azdat)<-gsub("X","v",names(azdat))
Jim
Always happy to help an ANZAC (bad joke)
On Wed, Oct 6, 2021 at 3:15 AM Anne Zach <anne.zach.zach at gmail.com> wrote:
Dear R users,
I have a dataframe that contains several variables, among which 105
correspond to scores on certain trials. Unfortunately, when I imported this
dataframe into R, I realised that the variable names corresponding to each
trial begin with digits, which violates R naming conventions.
I am trying to relabel these variables by adding a 'v' as a prefix to each
of them, I'd like to use tidyverse, but I am struggling with this process
of renaming. When I run this chunk of code, no error occurs but my
variables are not renamed. I'm fairly new to R and I can't understand what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
I appreciate if you can help.
Best,
Anne
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hello, Thank you for your help, I really appreciate it, I was able to solve the problem! Here is a part of my dataframe (just in case...):
dput( head( behavioral_df) )
structure(list(ID = c(1, 2, 3, 4, 5, 6), DOB = c("9/53/1959",
"4/8/1953", "2/21/1961", "10/11/1948", "9/4/1962", "8/22/1953"
), startpoint = c(2.33, 2.44, 1.57, 3.1, 2.78, 1.89), endpoint = c(3.5,
4, 2.4, 4.02, 3.98, 2.1), `1_t1_start` = c(1, 1, 1, 1, 1, 1),
`1_T1` = c(4, 7, 2, 3, 3, 5), `2_T2_start` = c(2.67, 3.3,
2.45, 2.2, 1.9, 2.6), `2_T2` = c(5, 5, 6, 4, 5, 3), `3_T3_start` =
c(4.76,
5.1, 3.87, 3.61, 2.83, 3.7), `3_T3` = c(7, 3, 4, 2, 5, 3),
`4_T4_start` = c(6.09, 6.99, 5.21, 5.19, 5.02, 6.34), `4_T4` = c(4,
5, 5, 4, 3, 6)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
Also, I checked my options and I have my email set to plain text...
Best,
Anne
On Tue, Oct 5, 2021 at 3:12 PM Jim Lemon <drjimlemon at gmail.com> wrote:
Hi Anne,
As mentioned above, you may have to do nothing. Here is an example
that might clarify that:
azdat<-read.table(text="subject 1 2 3
1 10 20 30
2 11 22 33",
header=TRUE,stringsAsFactors=FALSE)
azdat
subject X1 X2 X3
1 1 10 20 30
2 2 11 22 33
As you can see, R simply prepends an "X" to numeric names. If you
really want a "v":
names(azdat)<-gsub("X","v",names(azdat))
Jim
Always happy to help an ANZAC (bad joke)
On Wed, Oct 6, 2021 at 3:15 AM Anne Zach <anne.zach.zach at gmail.com> wrote:
Dear R users, I have a dataframe that contains several variables, among which 105 correspond to scores on certain trials. Unfortunately, when I imported
this
dataframe into R, I realised that the variable names corresponding to
each
trial begin with digits, which violates R naming conventions. I am trying to relabel these variables by adding a 'v' as a prefix to
each
of them, I'd like to use tidyverse, but I am struggling with this process of renaming. When I run this chunk of code, no error occurs but my variables are not renamed. I'm fairly new to R and I can't understand
what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
I appreciate if you can help.
Best,
Anne
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Also, I checked my options and I have my email set to plain text...
The warning inserted by the mailing list at the bottom of your message below
[[alternative HTML version deleted]]
indicates that you may still need to understand your mail client better.
On October 6, 2021 10:30:49 AM PDT, Anne Zach <anne.zach.zach at gmail.com> wrote:
Hello, Thank you for your help, I really appreciate it, I was able to solve the problem! Here is a part of my dataframe (just in case...):
dput( head( behavioral_df) )
structure(list(ID = c(1, 2, 3, 4, 5, 6), DOB = c("9/53/1959",
"4/8/1953", "2/21/1961", "10/11/1948", "9/4/1962", "8/22/1953"
), startpoint = c(2.33, 2.44, 1.57, 3.1, 2.78, 1.89), endpoint = c(3.5,
4, 2.4, 4.02, 3.98, 2.1), `1_t1_start` = c(1, 1, 1, 1, 1, 1),
`1_T1` = c(4, 7, 2, 3, 3, 5), `2_T2_start` = c(2.67, 3.3,
2.45, 2.2, 1.9, 2.6), `2_T2` = c(5, 5, 6, 4, 5, 3), `3_T3_start` =
c(4.76,
5.1, 3.87, 3.61, 2.83, 3.7), `3_T3` = c(7, 3, 4, 2, 5, 3),
`4_T4_start` = c(6.09, 6.99, 5.21, 5.19, 5.02, 6.34), `4_T4` = c(4,
5, 5, 4, 3, 6)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
Also, I checked my options and I have my email set to plain text...
Best,
Anne
On Tue, Oct 5, 2021 at 3:12 PM Jim Lemon <drjimlemon at gmail.com> wrote:
Hi Anne,
As mentioned above, you may have to do nothing. Here is an example
that might clarify that:
azdat<-read.table(text="subject 1 2 3
1 10 20 30
2 11 22 33",
header=TRUE,stringsAsFactors=FALSE)
azdat
subject X1 X2 X3
1 1 10 20 30
2 2 11 22 33
As you can see, R simply prepends an "X" to numeric names. If you
really want a "v":
names(azdat)<-gsub("X","v",names(azdat))
Jim
Always happy to help an ANZAC (bad joke)
On Wed, Oct 6, 2021 at 3:15 AM Anne Zach <anne.zach.zach at gmail.com> wrote:
Dear R users, I have a dataframe that contains several variables, among which 105 correspond to scores on certain trials. Unfortunately, when I imported
this
dataframe into R, I realised that the variable names corresponding to
each
trial begin with digits, which violates R naming conventions. I am trying to relabel these variables by adding a 'v' as a prefix to
each
of them, I'd like to use tidyverse, but I am struggling with this process of renaming. When I run this chunk of code, no error occurs but my variables are not renamed. I'm fairly new to R and I can't understand
what
I'm doing wrong.
```{r}
behavioral_df <- behavioral_df %>% rename_with(.fn = ~paste0("v"),
starts_with('^\\d'))
```
I appreciate if you can help.
Best,
Anne
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Sent from my phone. Please excuse my brevity.