everyone
I brought some new trivial questions to ask. I've searched, but I failed to look for the most efficient answer.
The string(or list) I would like to make looks like:
c("who","expr1","who","expr2",...,"who","expr100")
How can I efficiently make this string using loop(or another function) in R?
Thank you~!!
At least this is one choice:
c(rbind("who", paste0("expr", 1:100)))
Related
I'm unsure how to do this. If possible a walkthrough on this would be greatly appreciated! Just started a class that uses R and find somethings to be very complicated.
Thanks!
I have tried to just write simple types of loops but not sure how to tie in all of the functions I need to do.
In numpy if you have an array x you can access it's elements with a 'stride' (i.e. skipping some inbetween) like so: x[::2]. How can you do this in R with a vector? I've searched all over the internet and couldn't find an answer to something so simple, kind of surprising.
EDIT:
I just realized that you could use seq(), but is there no built-in method for doing this?
Ya so it turns out you just need to use
v[seq(to=length(v),by=stride)], just another quirk of R.
Though as #Igor F. mentioned they don't bother making it easier since array order is less important to statisticians. I imagine people are more likely to do something like sample(v,as.integer(length(v)/stride)) without being so verbose of course.
There is none, to my knowledge, but a hard-core R user (which I am not) would probably tell you that you are having a wrong approach. R is made for statistics, by statisticians. In their worldview, the order of the entries in an array or frame is irrelevant (or random), so there is no point in accessing them in a particular order.
I am trying to use update function on survey.design object. For instance, I want to create a variable that is the mean of 4 other variables, as follows
x1<-runif(3)
x2<-runif(3)
x3<-runif(3)
population=10000
testdf<-data.frame(x1,x2,x3,population)
testsvy<-svydesign(id=~1,weights=c(30,30,30),data=testdf)
testsvy<-update(testsvy,avg=mean(c(x1,x2,x3)))
However this returns a vector of the same number for every person. There must be something wrong. Alternatively I can modify on test$variables, but I don't feel that this is the easiest way...
OK I got the answer myself... Hope that it could be simpler since I type the object names three times...
testsvy<-update(testsvy,avg2=rowMeans(testsvy$variables[,c("x1","x2","x3")],na.rm=TRUE))
I have a list of dataframes called mylist.
This list contains 300 dataframes.
I need to subset each one of these for their complete cases.
I am very new to R, i started studying it 2 weeks ago, and I tried just this:
mylist[[1]] [[!(complete.cases(mylist[[1]])),]]
but it doesn't seem to work, as
Error in `[[.data.frame`(mylist[[1]], !(complete.cases(mylist[[1]])), :
argument "..2" is missing, with no default
I am searching on the web, but probably I am not asking the right question.
If someone would help me, even only reporting a link where I can take a look to the right function, I would be grateful.
Try this
lapply(mylist,function(x){x[complete.cases(x),]})
Also,
lapply(mylist, function(x) x[!rowSums(is.na(x)),])
Sorry, that I didn't wrote the sum function in latex. I tried $$ but that didn't work...
I am a beginner in R and I want to sum up:
Sum from i=0 to n by 1.054^i
I reserached the sum() function in R. However, it seems to me that is only can just add elements and not include an index or something.
So my question is: Can I solve that with a simple sum function or do I have to use a for loop for example?
I really appreciate your answer!
UPDATE
Here is a link to my sum(sorry that I cannot post it, but I need more reputation :(link )
In R most operations are vectorized, it requires changing the mindset a bit from other languages and for this question the answer is rather than looping, you simply do the entire operation on your sequence of numbers "at once":
sum(1.054^(0:n))