how to paste string in R [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 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.

Related

How could I make a function thar goes trough a vector and recognizes the signs of the numbers on it? [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
I was wondering if someone can help me with this, I need a function that goes trough a vector and recognizes the signs of the numbers inside, thats de basic idea, then I'll try to make some modifications so the function counts every number and shows something like "There are 2 positive numbers and 3 negatives"
What about the following approach (I added also functionality for zeros, remove this feature if it is not needed, or merge it with one of the other two conditions):
sign_function <- function(x){
count_pos_numbers = sum(x>0)
count_neg_numbers = sum(x<0)
count_zeros = sum(x==0)
sprintf("There are %d positive numbers, %d negative numbers, and %d zeros.", count_pos_numbers, count_neg_numbers , count_zeros)
}
I hope that I understood your question correctly.

if statement without an else or with an else? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am having some trouble interpreting a conditional here. Which one of these approaches would be better if I wanted this condition evaluated? Some code was shared with me that used this approach:
## Option 1
k1=2
a = "k1 is less than or equal 1"
if(k1 > 1) {
a = "k1 is greater than 1"
}
print(a)
But my instinct would be do something like this:
## Option 2
if(k1 > 1) {
a = "k1 is greater than 1"
} else {a = "k1 is less than or equal 1"}
print(a)
Is there any danger to using Option 1? Basically what I am asking here is what is the best practice and is either option more rigorous?
There isn't any danger to using option 1, as it works just as fine as option 2. The else statement just gives the ability to run a piece of code only if k1 is less than or equal to 1.
In a few cases, option 2 isn't so necessary especially if you only care about the condition being true. For example,
if(today's date == birthday){
System.out.println("Happy Birthday!");
} // no need for an else statement unless you want print out "today is not your b-day"
There is not much danger in using option1 , option2 is better as it allows other people to understand the code faster/easier since it is more visible to read.Also, option1 sometimes give error on complicated codes.
But both options are fine in this case.

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])
}

Pasting vector contents as objects' names [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 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

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, ]

Resources