Create a variable name with paste in R?
You can use assign
(doc) to change the value of perf.a1:
> assign(paste("perf.a", "1", sep=""),5)
> perf.a1
[1] 5
How to use object name in paste?
you can use assign
j <- network(i)
assign(paste0(i,'network',sep = '_'), j)
Dynamically create variable names and values using paste in R
With tidyverse
you could do the following. After selecting Q1 through Q5 (the columns needed to reproduce the new columns and omitting the already desired Q3 and Q4), you can put the data frame into long format (often preferable for future analyses). Row numbers rn
are assigned to track rows, and all columns are made character
so can be combined.
When in long format, the items sharing the same number (e.g., Q3a, Q3b, etc.) are grouped and then summarised, putting multiple values of the same group together separated by commas. Then, to get to your desired wide data frame, you can use pivot_wider
.
library(tidyverse)
df %>%
select(Q1:Q5) %>%
mutate_all(as.character) %>%
mutate(rn = row_number()) %>%
pivot_longer(cols = -rn,
names_to = c("item", "subitem"),
names_pattern = "Q(\\d+)(\\w*)") %>%
group_by(rn, item) %>%
summarise(value = toString(value)) %>%
pivot_wider(id_cols = rn,
names_from = item,
values_from = value, names_prefix = "Q") %>%
ungroup %>%
select(-rn)
Output
Q1 Q2 Q3 Q4 Q5
<chr> <chr> <chr> <chr> <chr>
1 1 2 a, b, c a, b, c, d e
2 2 3 a, b, c a, b, c, d f
3 2 1 c, d, e c, d, e, f g
R: Call a pasted variable name and use it as position argument
get
is like assign
, in that it lets you refer to variables by string instead of name.
r[get(paste0("p", x[1]))]=x[1]
But get
is one of those "flags" of something that could be written in a much clearer and safer way.
Paste a string of variable names into function in R
Something like this?
a1 = 1:2
a2 = 3:4
b1 = 5:6
b2 = 7:8
expand.grid(do.call(rbind, mget(paste("a", 1:2, sep = ""))),
do.call(rbind, mget(paste("b", 1:2, sep = ""))))
# Var1 Var2
#1 1 5
#2 3 5
#3 2 5
#4 4 5
#5 1 7
#6 3 7
#7 2 7
#8 4 7
#9 1 6
#10 3 6
#11 2 6
#12 4 6
#13 1 8
#14 3 8
#15 2 8
#16 4 8
Using recode() with variable names generated through paste()
You simply could put a named vector v
together with a list of your variables from mget
into Map
and subset it.
v <- c("Totally agree"=1, "Indifferent"=2, "Don't agree at all"=3)
Map(function(x, y) unname(y[x]), mget(ls(pattern="^item")), list(v))
# $item1
# [1] 3 1
#
# $item2
# [1] 2 1
Or, suppose you have a data frame like this one,
head(dat1)
# id item1 item2 x
# 1 1 Totally agree Totally agree 0.0356312
# 2 2 Totally agree Totally agree 1.3149588
# 3 3 Totally agree Indifferent 0.9781675
# 4 4 Totally agree Indifferent 0.8817912
# 5 5 Indifferent Indifferent 0.4822047
# 6 6 Indifferent Don't agree at all 0.9657529
then you may do this in a similar way. We may even simplify the code because we don't need the Map
to return unname
d objects anymore.
v1 <- c("Totally agree"=1, "Indifferent"=2, "Don't agree at all"=3)
item_nm <- c("item1", "item2")
dat1[item_nm] <- Map(`[`, list(v1), dat2[item_nm])
dat1
# id item1 item2 x
# 1 1 1 1 0.0356312
# 2 2 1 1 1.3149588
# 3 3 1 2 0.9781675
# 4 4 1 2 0.8817912
# 5 5 2 2 0.4822047
# 6 6 2 3 0.9657529
# 7 7 2 3 -0.8145709
# 8 8 1 1 0.2839578
# 9 9 3 1 -0.1616986
# 10 10 3 3 1.9355718
The second argument gets recycled for each Map
iteration (i.e. list(v1, v1)
would also work).
And more generally, for each column you want to recode numerically, list
one vector more in the second argument of Map
.
head(dat2)
# id item1 item2 x
# 1 1 Totally agree Always 0.0356312
# 2 2 Totally agree Always 1.3149588
# 3 3 Totally agree Both 0.9781675
# 4 4 Totally agree Both 0.8817912
# 5 5 Indifferent Both 0.4822047
# 6 6 Indifferent Never 0.9657529
v2 <- c("Always"=1, "Both"=2, "Never"=3)
dat2[item_nm] <- Map(`[`, list(v1, v2), dat2[item_nm])
dat2
# id item1 item2 x
# 1 1 1 1 0.0356312
# 2 2 1 1 1.3149588
# 3 3 1 2 0.9781675
# 4 4 1 2 0.8817912
# 5 5 2 2 0.4822047
# 6 6 2 3 0.9657529
# 7 7 2 3 -0.8145709
# 8 8 1 1 0.2839578
# 9 9 3 1 -0.1616986
# 10 10 3 3 1.9355718
Data:
dat1 <- structure(list(id = 1:10, item1 = c("Totally agree", "Totally agree",
"Totally agree", "Totally agree", "Indifferent", "Indifferent",
"Indifferent", "Totally agree", "Don't agree at all", "Don't agree at all"
), item2 = c("Totally agree", "Totally agree", "Indifferent",
"Indifferent", "Indifferent", "Don't agree at all", "Don't agree at all",
"Totally agree", "Totally agree", "Don't agree at all"), x = c(0.0356311982051355,
1.31495884897891, 0.978167526364279, 0.881791226863203, 0.482204688262918,
0.965752878105794, -0.814570938270238, 0.283957806364306, -0.161698647607024,
1.93557176599585)), class = "data.frame", row.names = c(NA, -10L
))
dat2 <- structure(list(id = 1:10, item1 = c("Totally agree", "Totally agree",
"Totally agree", "Totally agree", "Indifferent", "Indifferent",
"Indifferent", "Totally agree", "Don't agree at all", "Don't agree at all"
), item2 = c("Always", "Always", "Both", "Both", "Both", "Never",
"Never", "Always", "Always", "Never"), x = c(0.0356311982051355,
1.31495884897891, 0.978167526364279, 0.881791226863203, 0.482204688262918,
0.965752878105794, -0.814570938270238, 0.283957806364306, -0.161698647607024,
1.93557176599585)), class = "data.frame", row.names = c(NA, -10L
))
How to get the value of a variable name generated using paste in R
First consider fortune(312)
:
> library(fortunes)
> fortune(312)
The problem here is that the $ notation is a magical shortcut
and like any other magic if used incorrectly is likely to do
the programmatic equivalent of turning yourself into a toad.
-- Greg Snow (in response to a user that wanted to
access a column whose name is stored in y via x$y
rather than x[[y]])
R-help (February 2012)
Now look back at where you learned to subset using $
, if that source did not teach you that $
is a magical shortcut (or other phrasing) and that sometimes a shortcut is the longest path between 2 points, so use the regular indexing instead of the shortcut, then that person deserves to be hit up side the head with a wet fish. If they did teach that, then you should apologize to them for not paying attention (and now pay attention).
Also, for assigning to a vector, look at using sapply
instead of the for loop. You do not tell us what t
contains or what hurst
should be, but here is some sample code that shows an approach without get
:
> t <- lapply(1:15, function(i)list(Hs=i*10))
> names(t) <- paste0('V', 1:15)
> t$Other <- "don't grab me"
>
> hurst <- sapply( paste0("V", 1:15),
+ function(v) t[[c(v,"Hs")]])
> hurst
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14
10 20 30 40 50 60 70 80 90 100 110 120 130 140
V15
150
>
How may I refer to values of variable name with paste() function
I would use mget
and ls
.
mget(ls(pattern="cut"))
# $aado2cut
# [1] 20 180 360 460
#
# $agecut
# [1] 35 55 70 80
#
# $albumincut
# [1] 1.2 1.8 2.2 2.8 4.4
The result is a list containing all your variables verifying a certain pattern.
Related Topics
Pass Arguments to Dplyr Functions
Replace Multiple Letters With Accents With Gsub
R on Macos Error: Vector Memory Exhausted (Limit Reached)
Error - Replacement Has [X] Rows, Data Has [Y]
Ggplot2 Keep Unused Levels Barplot
Create Group Number For Contiguous Runs of Equal Values
Create a Co-Occurrence Matrix from Dummy-Coded Observations
How to Fill Geom_Polygon With Different Colors Above and Below Y = 0 (Or Any Other Value)
R Shiny - Add Tabpanel to Tabsetpanel Dynamically (With the Use of Renderui)
Change the Spacing of Tick Marks on the Axis of a Plot
How to Replace Na With Mean by Group/Subset
Dplyr Mutate Rowsums Calculations or Custom Functions
Displaying Text Below the Plot Generated by Ggplot2
How to Calculate the Co-Occurrence in the Table