jak utworzyć cytowane wyrażenie z łańcuchów
Biorąc pod uwagę wektor łańcuchów, chciałbym utworzyć wyrażenie bez cudzysłowów.
# eg, I would like to go from
c("string1", "string2")
# to... (notice the lack of '"' marks)
quote(list(string1, string2))
Napotykam pewne trudności z opuszczeniem cudzysłowów
input <- c("string1", "string2")
output <- paste0("quote(list(", paste(input, collapse=","), "))")
# not quite what I am looking for.
as.expression(output)
expression("quote(list(string1,string2))")
to jest do wykorzystania w danych.wybór kolumny tabeli, w razie potrzeby.
To, czego szukam, powinno pasować do danych.tabela:
library(data.table)
mydt <- data.table(id=1:3, string1=LETTERS[1:3], string2=letters[1:3])
result <- ????? # some.function.of(input)
> mydt[ , eval( result )]
string1 string2
1: A a
2: B b
3: C c
3 answers
Oto co bym zrobił:
## Create an example of a data.table "dt" whose columns you want to index
## using a character vector "xx"
library(data.table)
dt <- data.table(mtcars)
xx <- c("wt", "mpg")
## Construct a call object identical to that produced by quote(list("wt", "mpg"))
jj <- as.call(lapply(c("list", xx), as.symbol))
## Try it out
dt[1:5,eval(jj)]
# wt mpg
# 1: 2.620 21.0
# 2: 2.875 21.0
# 3: 2.320 22.8
# 4: 3.215 21.4
# 5: 3.440 18.7
Kiedy "obliczanie języka" w ten sposób, często pomocne jest przyjrzenie się strukturze obiektu, który próbujesz skonstruować. Na podstawie następujących informacji (a gdy już wiesz o as.call()
i as.symbol()
), Tworzenie pożądanego obiektu języka staje się bułką z masłem:
x <- quote(list(wt, mpg))
str(x)
# language list(wt, mpg)
class(x)
# [1] "call"
str(as.list(x))
# List of 3
# $ : symbol list
# $ : symbol wt
# $ : symbol mpg
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-05 00:04:40
Używam as.quoted
z pakietu plyr
outputString <- sprintf('list(%s)', paste(input, collapse = ', '))
library(plyr)
output <- as.quoted(outputString)[[1]]
mydt[, eval(output)]
string1 string2
1: A a
2: B b
3: C c
Jeśli jednak jest to po prostu wybór kolumny, możesz przekazać ciąg znaków i użyć with = FALSE
mydt[, input, with = FALSE]
string1 string2
1: A a
2: B b
3: C c
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-04 23:47:30
Znalazłem te odpowiedzi pomocne, ale niekompletne dla używania zmiennych i wielu linii w wyrażeniu. Aby utworzyć cytowane wyrażenie z łańcuchów, ze zmiennymi i wieloma liniami, użyj funkcji quote (), atop () i subsititute ():
# Prepare variables
samp_skewness = round(skewness(dv),2)
samp_kurtosis = round(kurtosis(dv),2)
samp_var = round(var(dv))
samp_summ <- summary(dv)
num_samples = length(dv)
# Prepare quotes containing math annotations
q1 = quote(paste(mu,"="))
q2 = quote(paste(sigma,"="))
q3 = quote(paste(gamma[1],"="))
q4 = quote(paste(gamma[2],"="))
# Use subsitition to construct the expression, passing in the variables and quoted expressions
title = substitute(atop(paste("Top Title, # samples: ", ns),
paste(e1,v1,", ",e2,v2,", ",e3,v3,", ",e4,v4)),
list(ns=num_samples,v1=round(samp_summ['Mean']),v2=samp_var,
v3=samp_skewness,v4=samp_kurtosis,e1=q1,e2=q2,e3=q3,e4=q4))
W ggplot: ...
labs(title = title) +
...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-04 18:38:37