Cannot use multi word variables in dplyr or am I missing something?Can dplyr summarise over several variables without listing each one?dplyr filter: Get rows with minimum of variable, but only the first if multiple minimadata.table vs dplyr: can one do something well the other can't or does poorly?dplyr - mutate: use dynamic variable namesCreate a ranking variable with dplyr?Change value of variable with dplyr“Adding missing grouping variables” message in dplyr in RHow to write loops “for” loops in R using dplyr syntaxgrouping in dplyr with missing columnsRepetitive filtering with multiple conditions without a loop
Why can't I share a one use code with anyone else?
How can I make dummy text (like lipsum) grey?
Refer a string as a field API name
Why is the marginal distribution/marginal probability described as "marginal"?
What would a Dragon have to exhale to cause rain?
I recently started my machine learning PhD and I have absolutely no idea what I'm doing
What color to choose as "danger" if the main color of my app is red
Why did nobody know who the Lord of this region was?
Why is Drogon so much better in battle than Rhaegal and Viserion?
When the match time is called, does the current turn end immediately?
Is it standard to have the first week's pay indefinitely withheld?
Why does string strummed with finger sound different from the one strummed with pick?
What are the effects of eating many berries from the Goodberry spell per day?
How to know the path of a particular software?
Do we see some Unsullied doing this in S08E05?
How do Ctrl+C and Ctrl+V work?
Why are there five extra turns in tournament Magic?
Deleting the same lines from a list
Iterate lines of string variable in bash
Would a "ring language" be possible?
How long do Aarakocra live?
Non-African Click Languages
Physically unpleasant work environment
What dog breeds survive the apocalypse for generations?
Cannot use multi word variables in dplyr or am I missing something?
Can dplyr summarise over several variables without listing each one?dplyr filter: Get rows with minimum of variable, but only the first if multiple minimadata.table vs dplyr: can one do something well the other can't or does poorly?dplyr - mutate: use dynamic variable namesCreate a ranking variable with dplyr?Change value of variable with dplyr“Adding missing grouping variables” message in dplyr in RHow to write loops “for” loops in R using dplyr syntaxgrouping in dplyr with missing columnsRepetitive filtering with multiple conditions without a loop
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Why doesn't dplyr
like this format of 'beta linalool' in my function as compared to beta.linalool?
It took me a few hours of troubleshooting to figure out what the problem was. Is there any way to use data where variables are labeled as more than one word or should I just move everything to the beta.linalool type format?
Everything I have learned has been from Programming with dplyr.
library(ggplot2)
library(readxl)
library(dplyr)
library(magrittr)
Data3<- read_excel("Desktop/Data3.xlsx")
Data3 %>% filter(Variety=="CS 420A"&`Red Blotch`=="-")%>% group_by(`Time Point`)%>%
summarise(m=mean(`beta linalool`),SD=sd(`beta linalool`))
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.00300 0.000117
2 Mid 0.00385 0.000353
3 Must 0.000254 0.00000633
4 Start 0.000785 0.000283
Now when I work it into a function:
cwine<-function(df,v,rb,c)
c<-enquo(c)
df %>% filter(Variety==v&`Red Blotch`==rb)%>%
group_by(`Time Point`) %>%
summarise_(m=mean(!!c),SD=sd(!!c)) %>%
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End NA NA
2 Mid NA NA
3 Must NA NA
4 Start NA NA
Warning messages:
1: In mean.default(~"beta linalool") :
argument is not numeric or logical: returning NA #this statement is repeated 4 more times
5: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
NAs introduced by coercion #this statement is repeated 4 more times
The problem lies in that beta linalool is typed in as 'beta linalool'. I figured this out by trying this methodology on the iris dataset and seeing that Petal.Length is not 'Petal Width':
my_function<-function(ds,x,y,c)
c<-enquo(c)
ds %>%filter(Sepal.Length>x&Sepal.Width<y) %>%
group_by(Species) %>%
summarise(m=mean(!!c),SD=sd(!!c))
my_function2(iris,5,4,Petal.Length)
# A tibble: 3 x 3
Species m SD
<fct> <dbl> <dbl>
1 setosa 1.53 0.157
2 versicolor 4.32 0.423
3 virginica 5.57 0.536
In fact my function works fine on a different variable:
> cwine(Data2,"CS 420A","-",nerol)
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.000453 0.0000338
2 Mid 0.000659 0.0000660
3 Must 0.000560 0.0000234
4 Start 0.000927 0.0000224
Is dplyr just that sensitive or am I missing something?
r function dplyr
add a comment |
Why doesn't dplyr
like this format of 'beta linalool' in my function as compared to beta.linalool?
It took me a few hours of troubleshooting to figure out what the problem was. Is there any way to use data where variables are labeled as more than one word or should I just move everything to the beta.linalool type format?
Everything I have learned has been from Programming with dplyr.
library(ggplot2)
library(readxl)
library(dplyr)
library(magrittr)
Data3<- read_excel("Desktop/Data3.xlsx")
Data3 %>% filter(Variety=="CS 420A"&`Red Blotch`=="-")%>% group_by(`Time Point`)%>%
summarise(m=mean(`beta linalool`),SD=sd(`beta linalool`))
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.00300 0.000117
2 Mid 0.00385 0.000353
3 Must 0.000254 0.00000633
4 Start 0.000785 0.000283
Now when I work it into a function:
cwine<-function(df,v,rb,c)
c<-enquo(c)
df %>% filter(Variety==v&`Red Blotch`==rb)%>%
group_by(`Time Point`) %>%
summarise_(m=mean(!!c),SD=sd(!!c)) %>%
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End NA NA
2 Mid NA NA
3 Must NA NA
4 Start NA NA
Warning messages:
1: In mean.default(~"beta linalool") :
argument is not numeric or logical: returning NA #this statement is repeated 4 more times
5: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
NAs introduced by coercion #this statement is repeated 4 more times
The problem lies in that beta linalool is typed in as 'beta linalool'. I figured this out by trying this methodology on the iris dataset and seeing that Petal.Length is not 'Petal Width':
my_function<-function(ds,x,y,c)
c<-enquo(c)
ds %>%filter(Sepal.Length>x&Sepal.Width<y) %>%
group_by(Species) %>%
summarise(m=mean(!!c),SD=sd(!!c))
my_function2(iris,5,4,Petal.Length)
# A tibble: 3 x 3
Species m SD
<fct> <dbl> <dbl>
1 setosa 1.53 0.157
2 versicolor 4.32 0.423
3 virginica 5.57 0.536
In fact my function works fine on a different variable:
> cwine(Data2,"CS 420A","-",nerol)
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.000453 0.0000338
2 Mid 0.000659 0.0000660
3 Must 0.000560 0.0000234
4 Start 0.000927 0.0000224
Is dplyr just that sensitive or am I missing something?
r function dplyr
2
Hadley anddplyr
developers generally advocate for the naming convention:my_function_name
andmy_column_name
to avoid such problems as occur when you have spaces in a column name.
– NelsonGon
May 5 at 5:40
add a comment |
Why doesn't dplyr
like this format of 'beta linalool' in my function as compared to beta.linalool?
It took me a few hours of troubleshooting to figure out what the problem was. Is there any way to use data where variables are labeled as more than one word or should I just move everything to the beta.linalool type format?
Everything I have learned has been from Programming with dplyr.
library(ggplot2)
library(readxl)
library(dplyr)
library(magrittr)
Data3<- read_excel("Desktop/Data3.xlsx")
Data3 %>% filter(Variety=="CS 420A"&`Red Blotch`=="-")%>% group_by(`Time Point`)%>%
summarise(m=mean(`beta linalool`),SD=sd(`beta linalool`))
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.00300 0.000117
2 Mid 0.00385 0.000353
3 Must 0.000254 0.00000633
4 Start 0.000785 0.000283
Now when I work it into a function:
cwine<-function(df,v,rb,c)
c<-enquo(c)
df %>% filter(Variety==v&`Red Blotch`==rb)%>%
group_by(`Time Point`) %>%
summarise_(m=mean(!!c),SD=sd(!!c)) %>%
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End NA NA
2 Mid NA NA
3 Must NA NA
4 Start NA NA
Warning messages:
1: In mean.default(~"beta linalool") :
argument is not numeric or logical: returning NA #this statement is repeated 4 more times
5: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
NAs introduced by coercion #this statement is repeated 4 more times
The problem lies in that beta linalool is typed in as 'beta linalool'. I figured this out by trying this methodology on the iris dataset and seeing that Petal.Length is not 'Petal Width':
my_function<-function(ds,x,y,c)
c<-enquo(c)
ds %>%filter(Sepal.Length>x&Sepal.Width<y) %>%
group_by(Species) %>%
summarise(m=mean(!!c),SD=sd(!!c))
my_function2(iris,5,4,Petal.Length)
# A tibble: 3 x 3
Species m SD
<fct> <dbl> <dbl>
1 setosa 1.53 0.157
2 versicolor 4.32 0.423
3 virginica 5.57 0.536
In fact my function works fine on a different variable:
> cwine(Data2,"CS 420A","-",nerol)
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.000453 0.0000338
2 Mid 0.000659 0.0000660
3 Must 0.000560 0.0000234
4 Start 0.000927 0.0000224
Is dplyr just that sensitive or am I missing something?
r function dplyr
Why doesn't dplyr
like this format of 'beta linalool' in my function as compared to beta.linalool?
It took me a few hours of troubleshooting to figure out what the problem was. Is there any way to use data where variables are labeled as more than one word or should I just move everything to the beta.linalool type format?
Everything I have learned has been from Programming with dplyr.
library(ggplot2)
library(readxl)
library(dplyr)
library(magrittr)
Data3<- read_excel("Desktop/Data3.xlsx")
Data3 %>% filter(Variety=="CS 420A"&`Red Blotch`=="-")%>% group_by(`Time Point`)%>%
summarise(m=mean(`beta linalool`),SD=sd(`beta linalool`))
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.00300 0.000117
2 Mid 0.00385 0.000353
3 Must 0.000254 0.00000633
4 Start 0.000785 0.000283
Now when I work it into a function:
cwine<-function(df,v,rb,c)
c<-enquo(c)
df %>% filter(Variety==v&`Red Blotch`==rb)%>%
group_by(`Time Point`) %>%
summarise_(m=mean(!!c),SD=sd(!!c)) %>%
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End NA NA
2 Mid NA NA
3 Must NA NA
4 Start NA NA
Warning messages:
1: In mean.default(~"beta linalool") :
argument is not numeric or logical: returning NA #this statement is repeated 4 more times
5: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
NAs introduced by coercion #this statement is repeated 4 more times
The problem lies in that beta linalool is typed in as 'beta linalool'. I figured this out by trying this methodology on the iris dataset and seeing that Petal.Length is not 'Petal Width':
my_function<-function(ds,x,y,c)
c<-enquo(c)
ds %>%filter(Sepal.Length>x&Sepal.Width<y) %>%
group_by(Species) %>%
summarise(m=mean(!!c),SD=sd(!!c))
my_function2(iris,5,4,Petal.Length)
# A tibble: 3 x 3
Species m SD
<fct> <dbl> <dbl>
1 setosa 1.53 0.157
2 versicolor 4.32 0.423
3 virginica 5.57 0.536
In fact my function works fine on a different variable:
> cwine(Data2,"CS 420A","-",nerol)
# A tibble: 4 x 3
`Time Point` m SD
<chr> <dbl> <dbl>
1 End 0.000453 0.0000338
2 Mid 0.000659 0.0000660
3 Must 0.000560 0.0000234
4 Start 0.000927 0.0000224
Is dplyr just that sensitive or am I missing something?
r function dplyr
r function dplyr
edited May 5 at 5:42
NelsonGon
5,0514934
5,0514934
asked May 5 at 5:11
andrewjcandrewjc
383
383
2
Hadley anddplyr
developers generally advocate for the naming convention:my_function_name
andmy_column_name
to avoid such problems as occur when you have spaces in a column name.
– NelsonGon
May 5 at 5:40
add a comment |
2
Hadley anddplyr
developers generally advocate for the naming convention:my_function_name
andmy_column_name
to avoid such problems as occur when you have spaces in a column name.
– NelsonGon
May 5 at 5:40
2
2
Hadley and
dplyr
developers generally advocate for the naming convention: my_function_name
and my_column_name
to avoid such problems as occur when you have spaces in a column name.– NelsonGon
May 5 at 5:40
Hadley and
dplyr
developers generally advocate for the naming convention: my_function_name
and my_column_name
to avoid such problems as occur when you have spaces in a column name.– NelsonGon
May 5 at 5:40
add a comment |
1 Answer
1
active
oldest
votes
One option would be convert it to sym
bol and evaluate it
library(tidyverse)
cwine <- function(df,v,rb,c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!!rlang::sym(c)),
SD = sd(!! rlang::sym(c)))
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
Also, if we want to pass it by converting to quosure (enquo
), it works, when we pass the variable name with backquotes (usually, unquoted version works, but here there is a space between words and to evaluate it as it is, backquote is needed)
cwine <- function(df,v,rb,c)
c1 <- enquo(c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!! c1 ),
SD = sd(!! c1))
cwine(Data3,"CS 420A","-",`beta linalool`)
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
data
set.seed(24)
Data3 <- tibble(Variety = sample(c("CS 420A", "CS 410A"), 20, replace = TRUE),
`Red Blotch` = sample(c("-", "+"), 20, replace = TRUE),
`Time Point` = sample(1:4, 20, replace = TRUE),
`beta linalool` = rnorm(20))
Unfortunately that resulted in "Error: Only strings can be converted to symbols Callrlang::last_error()
to see a backtrace"
– andrewjc
May 5 at 5:19
1
@andrewjc I found some typossummarise_
in your code. Fixed it, can you try it now. I tried with a reproducible example and it is working fine for me
– akrun
May 5 at 5:34
1
I thinksummarise_
and the like were designed for SE. However, they seem to have lost favour(I think). They are infact deprecated.
– NelsonGon
May 5 at 5:43
1
@NelsonGon Yes, i misstyped as typo. the_
suffix was earlier used for NSE. Now, therlang
functions are mostly used for that purpose and convenient options withsummarise_at/mutate_at/group_by_at
takes a string column names and evaluates
– akrun
May 5 at 5:47
2
@akrun thank you for taking the time on this, what you recommended works well.
– andrewjc
May 5 at 6:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55989035%2fcannot-use-multi-word-variables-in-dplyr-or-am-i-missing-something%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
One option would be convert it to sym
bol and evaluate it
library(tidyverse)
cwine <- function(df,v,rb,c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!!rlang::sym(c)),
SD = sd(!! rlang::sym(c)))
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
Also, if we want to pass it by converting to quosure (enquo
), it works, when we pass the variable name with backquotes (usually, unquoted version works, but here there is a space between words and to evaluate it as it is, backquote is needed)
cwine <- function(df,v,rb,c)
c1 <- enquo(c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!! c1 ),
SD = sd(!! c1))
cwine(Data3,"CS 420A","-",`beta linalool`)
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
data
set.seed(24)
Data3 <- tibble(Variety = sample(c("CS 420A", "CS 410A"), 20, replace = TRUE),
`Red Blotch` = sample(c("-", "+"), 20, replace = TRUE),
`Time Point` = sample(1:4, 20, replace = TRUE),
`beta linalool` = rnorm(20))
Unfortunately that resulted in "Error: Only strings can be converted to symbols Callrlang::last_error()
to see a backtrace"
– andrewjc
May 5 at 5:19
1
@andrewjc I found some typossummarise_
in your code. Fixed it, can you try it now. I tried with a reproducible example and it is working fine for me
– akrun
May 5 at 5:34
1
I thinksummarise_
and the like were designed for SE. However, they seem to have lost favour(I think). They are infact deprecated.
– NelsonGon
May 5 at 5:43
1
@NelsonGon Yes, i misstyped as typo. the_
suffix was earlier used for NSE. Now, therlang
functions are mostly used for that purpose and convenient options withsummarise_at/mutate_at/group_by_at
takes a string column names and evaluates
– akrun
May 5 at 5:47
2
@akrun thank you for taking the time on this, what you recommended works well.
– andrewjc
May 5 at 6:07
add a comment |
One option would be convert it to sym
bol and evaluate it
library(tidyverse)
cwine <- function(df,v,rb,c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!!rlang::sym(c)),
SD = sd(!! rlang::sym(c)))
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
Also, if we want to pass it by converting to quosure (enquo
), it works, when we pass the variable name with backquotes (usually, unquoted version works, but here there is a space between words and to evaluate it as it is, backquote is needed)
cwine <- function(df,v,rb,c)
c1 <- enquo(c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!! c1 ),
SD = sd(!! c1))
cwine(Data3,"CS 420A","-",`beta linalool`)
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
data
set.seed(24)
Data3 <- tibble(Variety = sample(c("CS 420A", "CS 410A"), 20, replace = TRUE),
`Red Blotch` = sample(c("-", "+"), 20, replace = TRUE),
`Time Point` = sample(1:4, 20, replace = TRUE),
`beta linalool` = rnorm(20))
Unfortunately that resulted in "Error: Only strings can be converted to symbols Callrlang::last_error()
to see a backtrace"
– andrewjc
May 5 at 5:19
1
@andrewjc I found some typossummarise_
in your code. Fixed it, can you try it now. I tried with a reproducible example and it is working fine for me
– akrun
May 5 at 5:34
1
I thinksummarise_
and the like were designed for SE. However, they seem to have lost favour(I think). They are infact deprecated.
– NelsonGon
May 5 at 5:43
1
@NelsonGon Yes, i misstyped as typo. the_
suffix was earlier used for NSE. Now, therlang
functions are mostly used for that purpose and convenient options withsummarise_at/mutate_at/group_by_at
takes a string column names and evaluates
– akrun
May 5 at 5:47
2
@akrun thank you for taking the time on this, what you recommended works well.
– andrewjc
May 5 at 6:07
add a comment |
One option would be convert it to sym
bol and evaluate it
library(tidyverse)
cwine <- function(df,v,rb,c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!!rlang::sym(c)),
SD = sd(!! rlang::sym(c)))
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
Also, if we want to pass it by converting to quosure (enquo
), it works, when we pass the variable name with backquotes (usually, unquoted version works, but here there is a space between words and to evaluate it as it is, backquote is needed)
cwine <- function(df,v,rb,c)
c1 <- enquo(c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!! c1 ),
SD = sd(!! c1))
cwine(Data3,"CS 420A","-",`beta linalool`)
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
data
set.seed(24)
Data3 <- tibble(Variety = sample(c("CS 420A", "CS 410A"), 20, replace = TRUE),
`Red Blotch` = sample(c("-", "+"), 20, replace = TRUE),
`Time Point` = sample(1:4, 20, replace = TRUE),
`beta linalool` = rnorm(20))
One option would be convert it to sym
bol and evaluate it
library(tidyverse)
cwine <- function(df,v,rb,c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!!rlang::sym(c)),
SD = sd(!! rlang::sym(c)))
cwine(Data3,"CS 420A","-",'beta linalool')
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
Also, if we want to pass it by converting to quosure (enquo
), it works, when we pass the variable name with backquotes (usually, unquoted version works, but here there is a space between words and to evaluate it as it is, backquote is needed)
cwine <- function(df,v,rb,c)
c1 <- enquo(c)
df %>%
filter(Variety==v & `Red Blotch` == rb)%>%
group_by(`Time Point`) %>%
summarise(m = mean(!! c1 ),
SD = sd(!! c1))
cwine(Data3,"CS 420A","-",`beta linalool`)
# A tibble: 2 x 3
# `Time Point` m SD
# <int> <dbl> <dbl>
#1 2 -2.11 2.23
#2 4 0.0171 NA
data
set.seed(24)
Data3 <- tibble(Variety = sample(c("CS 420A", "CS 410A"), 20, replace = TRUE),
`Red Blotch` = sample(c("-", "+"), 20, replace = TRUE),
`Time Point` = sample(1:4, 20, replace = TRUE),
`beta linalool` = rnorm(20))
edited May 5 at 5:37
answered May 5 at 5:14
akrunakrun
432k13223311
432k13223311
Unfortunately that resulted in "Error: Only strings can be converted to symbols Callrlang::last_error()
to see a backtrace"
– andrewjc
May 5 at 5:19
1
@andrewjc I found some typossummarise_
in your code. Fixed it, can you try it now. I tried with a reproducible example and it is working fine for me
– akrun
May 5 at 5:34
1
I thinksummarise_
and the like were designed for SE. However, they seem to have lost favour(I think). They are infact deprecated.
– NelsonGon
May 5 at 5:43
1
@NelsonGon Yes, i misstyped as typo. the_
suffix was earlier used for NSE. Now, therlang
functions are mostly used for that purpose and convenient options withsummarise_at/mutate_at/group_by_at
takes a string column names and evaluates
– akrun
May 5 at 5:47
2
@akrun thank you for taking the time on this, what you recommended works well.
– andrewjc
May 5 at 6:07
add a comment |
Unfortunately that resulted in "Error: Only strings can be converted to symbols Callrlang::last_error()
to see a backtrace"
– andrewjc
May 5 at 5:19
1
@andrewjc I found some typossummarise_
in your code. Fixed it, can you try it now. I tried with a reproducible example and it is working fine for me
– akrun
May 5 at 5:34
1
I thinksummarise_
and the like were designed for SE. However, they seem to have lost favour(I think). They are infact deprecated.
– NelsonGon
May 5 at 5:43
1
@NelsonGon Yes, i misstyped as typo. the_
suffix was earlier used for NSE. Now, therlang
functions are mostly used for that purpose and convenient options withsummarise_at/mutate_at/group_by_at
takes a string column names and evaluates
– akrun
May 5 at 5:47
2
@akrun thank you for taking the time on this, what you recommended works well.
– andrewjc
May 5 at 6:07
Unfortunately that resulted in "Error: Only strings can be converted to symbols Call
rlang::last_error()
to see a backtrace"– andrewjc
May 5 at 5:19
Unfortunately that resulted in "Error: Only strings can be converted to symbols Call
rlang::last_error()
to see a backtrace"– andrewjc
May 5 at 5:19
1
1
@andrewjc I found some typos
summarise_
in your code. Fixed it, can you try it now. I tried with a reproducible example and it is working fine for me– akrun
May 5 at 5:34
@andrewjc I found some typos
summarise_
in your code. Fixed it, can you try it now. I tried with a reproducible example and it is working fine for me– akrun
May 5 at 5:34
1
1
I think
summarise_
and the like were designed for SE. However, they seem to have lost favour(I think). They are infact deprecated.– NelsonGon
May 5 at 5:43
I think
summarise_
and the like were designed for SE. However, they seem to have lost favour(I think). They are infact deprecated.– NelsonGon
May 5 at 5:43
1
1
@NelsonGon Yes, i misstyped as typo. the
_
suffix was earlier used for NSE. Now, the rlang
functions are mostly used for that purpose and convenient options with summarise_at/mutate_at/group_by_at
takes a string column names and evaluates– akrun
May 5 at 5:47
@NelsonGon Yes, i misstyped as typo. the
_
suffix was earlier used for NSE. Now, the rlang
functions are mostly used for that purpose and convenient options with summarise_at/mutate_at/group_by_at
takes a string column names and evaluates– akrun
May 5 at 5:47
2
2
@akrun thank you for taking the time on this, what you recommended works well.
– andrewjc
May 5 at 6:07
@akrun thank you for taking the time on this, what you recommended works well.
– andrewjc
May 5 at 6:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55989035%2fcannot-use-multi-word-variables-in-dplyr-or-am-i-missing-something%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Hadley and
dplyr
developers generally advocate for the naming convention:my_function_name
andmy_column_name
to avoid such problems as occur when you have spaces in a column name.– NelsonGon
May 5 at 5:40