Pasting vector contents as objects' names [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say I have two objects in my environment, obj_1 and obj_2. And I also have a vector of their names vec <- c("obj_1", "obj_2").
Now, I want to exclude something from that vector, let's say vec <- vec[-c(2)], and then to pass the rest to another custom function -- i.e. somehow paste the vector contents the way that it would correspond to an actual object in an my environment.
How can it be done?

Set up your environment:
obj_1 <- 1:10
obj_2 <- 11:20
vec <- c("obj_1", "obj_2")
Get the name of the object that you want:
vec <- vec[-2]
Get the object:
get(vec)
Also look at mget

Related

how to add add a new variable to multiple data.frame at once without creating a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If I have multiple data.frame in my environment with name _OK, and I would like add ID=1 into each of them. Is it a way that i can do it at once instead of one by one? I know that i can use mget to build a list for the data.frame ended with _OK. But I would like to know whether there is a way to skip the list. USing list, I will be ended up with two set of data.
Any suggestion?
We could do that in a for loop with assign
for(obj in ls(pattern = '_OK')) {
tmp <- get(obj)
tmp$ID <- 1
assign(obj, tmp)
}
Or another option is mget to return the datasets into a list, transform each element by looping over the list and update the objects in the global environment with list2env
list2env(lapply(mget(ls(pattern = '_OK')), transform, ID = 1), .GlobalEnv)

How to change and add new column with new ID names [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to change column in many files. I have files in which my names in ID column are written wrong. I prepared excel files with 2 columns "wrong ID" and "new ID" and now I want to change it in every files.
for ex.
wrong ID new ID
CALU1 calu-1
I have almost 300 different ID.
We can keep the files in a list after reading them and then use rename_at
library(dplyr)
lst1 <- lapply(files, function(x) {
x1 <- read.csv(x)
x1 %>%
rename_at(vars(df$`wrong ID`), ~ df$`new ID`)
})

for loop with dimension of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a code,and i am going to do a for loop on two gamma distributions.
Given the list of shape parameter, and i name them "d" then i put in d[1] and d[2] in the random gamma function.
I have simplified what I wish to ask here. when i code d[1] in R, output will be the first vector,while when i code d[2] in R, output will be the second vector.
I have a bit lose then how will it iterate if i using for loop for d ?
*
List_1 <- list(c(4,16),c(16/9,4),c(1,16/9),c(.64,1),c(4/9,.64))
for ( d in List_1) ##first parameter is for gamma.1, second is for gamma.2
{
x<-rgamma(25,d[1],1)
y<-rgamma(25,d[2],1)
t<-t.test(x,y)$p.value
}*
I am sorry if i do ask a silly question. Thanks in advance.
In R it is better to avoid for loops due to their poor performance. Since you are starting with a list lapply is a good start:
lapply(List_1, FUN=function(x){t.test(rgamma(25,x[1],1), rgamma(25,x[2],1))$p.value})
The apply function takes your list and then uses the gamma function on the 2 parameters within the t.test. The result will be a list of the five p values, one for each pair
Your code runs fine. I am actually not sure what you are asking here. You can just use print to find the iteratives, if that is what you want, like:
for (d in List_1){
print(d[1])
print(d[2])
}

Remove first row conditionally [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a very simple problem and I'll probably catch flame for asking this, but I've looked everywhere and I can't find the answer... Or a different approach to my code.
I need to remove the first row of a data frame if the value of the first row and third column are equal to one.
This is what I have so far:
if (foo[1, 3] == 1) {
foo <- foo[-1, ]
}
Is there a different way to do this using only bracket subsetting (avoiding using an if statement)?
Edit:
Edited for clarity.
The code you wrote doesn't remove the first row permanently, it only prints it out. Change that with foo<-foo[-1, ]
Additionally, the code within the if-statement brackets is only one line anyway, you don't technically need them, but some like them for clarity purposes
if (foo[1, 3] == 1) foo <- foo[-1, ]

how to paste string in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to paste 4 strings together, the code is here:
urlstring<-"lee/"
code<-read.csv("D:\\list.csv",sep="\n",head=FALSE)
for(y in code){count<-1
while(count<3){
mydate<-Sys.Date()-count
filename<-paste(urlstring,mydate,"&symbol=",y,sep="")
print(filename)
count<-count+1
}
}
my question is why the output is, date is changing firstly :
lee/date=2013-11-14&symbol=1
lee/date=2013-11-14&symbol=2
lee/date=2013-11-13&symbol=1
lee/date=2013-11-13&symbol=2
but in my opinion,the result should be this,the value of code should be changed firstly:
lee/date=2013-11-14&symbol=1
lee/date=2013-11-13&symbol=1
lee/date=2013-11-14&symbol=2
lee/date=2013-11-13&symbol=2
Like #mnel said. for code = 1:2 your code yields the correct results. Note that the nested for-while loop is not needed, a vectorized solution uses less code is and is often faster:
code = rep(1:2, each = 2)
mydate = Sys.Date() - code
sprintf('lee/date=%s&symbol=%d', mydate, code)
[1] "lee/date=2013-11-14&symbol=1" "lee/date=2013-11-14&symbol=1"
[3] "lee/date=2013-11-13&symbol=2" "lee/date=2013-11-13&symbol=2"
This solution is called vectorized because when sprintf is used with vectors, the result is also a vector, without explicitily using a loop.

Resources