I am generating dynamic variables names like P_1_Onsets_PRH, p_2_Onsets_PRH, etc. in a for loop. In this same loop, I'd like to read these variable names and generate corresponding matrices P1_Durations_PRH, etc. having the same number of elements as the respective Onset matrix.
for (i in 1:nrow(LabviewFiles)){
assign(x = paste("P",i , "Onsets_PRH", sep = "_"), value = t(subset.data.frame(All_Phase, All_Phase$Phase==i) %>%
filter(CONDITIONS == "NULL_TRIAL",
MISC_REWARD == 1,
MISC_PASSIVE_FAILED == 1) %>%
select(Feedback_onset)))
assign(x = paste("P",i , "Durations_PRH", sep = "_"), value = t(rep(0.5, times = length(noquote(paste("P",i , "Onsets_PRH", sep = "_"))))))
}
How do I read the length of matrix 'P_i_Onsets_PRH'?
I'm a newbie to R. Any help is appreciated.
You may use get
to do this -
library(dplyr)
for (i in 1:nrow(LabviewFiles)){
assign(x = paste("P",i , "Onsets_PRH", sep = "_"), value = t(subset.data.frame(All_Phase, All_Phase$Phase==i) %>%
filter(CONDITIONS == "NULL_TRIAL",
MISC_REWARD == 1,
MISC_PASSIVE_FAILED == 1) %>%
select(Feedback_onset)))
assign(x = paste("P",i , "Durations_PRH", sep = "_"), value = t(rep(0.5, times = length(get(paste("P",i , "Onsets_PRH", sep = "_"))))))
}
Note that using assign
and creating variables in global environment is discouraged. You may also read Why is using assign bad?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.