Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
All, I wrote this function but it has flaws:
X=cbind(rep(1:5,4),rep(c(1,2,4,8,16),2),c(3.4,4,45,6,4,2,36,4,34,7,8,0,2,4,5,7,9,12,23,1))
getXbarl<-function(Y,l){
xbar=tapply(Y,l,mean)
return(matrix(xbar[l]))#***
}
#It works for the first row:
getXbarl(X[,3],X[,1])
#but not the second row, because the factors are no longer 1:5 here.
getXbarl(X[,3],X[,2])
Please help me write a fix. The issue is that for xbar[l]***, it no longer corresponds to the index.
I think you need ave
ave(X[,3], X[,2])
As a function
getXbarl2 <- function(Y,l) matrix(ave(Y,l))
identical(getXbarl(X[,3], X[,1]), getXbarl2(X[,3], X[,1]))
#[1] TRUE
getXbarl<-function(Y,l){
xbar=tapply(Y,l,mean)
l2=factor(l,labels=1:length(xbar))
return(matrix(xbar[l2]))
}
X=cbind(rep(1:5,4),rep(c(1,2,4,8,16),2),c(3.4,4,45,6,4,2,36,4,34,7,8,0,2,4,5,7,9,12,23,1))
getXbarl(X[,3],X[,1])
getXbarl(X[,3],X[,2])
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to calculate the standard deviation from scratch in R (I do know R has a build-in function for that), but I'm not getting the correct results, so I don't know what I'm doing wrong.
My.SD<-function(X) {
M<-(sum(X)/length(X))
my.var<-sum((X-M)^2)
StanDev<-sqrt(my.var/length(X))
print(StanDev)
}
I guess you might need to use (length(X)-1) rather than length(x), i.e.,
My.SD<-function(X) {
M<-(sum(X)/length(X))
my.var<-sum((X-M)^2)
StanDev<-sqrt(my.var/(length(X)-1))
print(StanDev)
}
since here it should be sample standard deviation.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have some values stored in a dataframe and I would like to take them and use them as variable names. How do I do that without writing/Hardcode the variable name?
You can use the assign() function. Here is an example:
data <- data.frame("Name"=c("John", "Evie", "Graham", "Mary"),
"Age"=c(13,43,26,17), stringsAsFactors=FALSE)
for(row in 1:nrow(data)){
assign(x=data[row, "Name"], value=data[row, "Age"])
}
print(Evie)
$Evie
[1] 43
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am struggling to create a variable based on a condition in my data frame.
I think the code is quite self-explanatory.
DT$xp_ratio_y<- apply(DT,1,function(x)
if(DT$driv_y_add_flg==1) {
x=DT$driv_y_experience/DT$driv_y_age
} else {
x=0
}
)
I think you're making it too complicated. Just calculate for all then remove those you don't want:
DT$xp_ratio_y <- DT$driv_y_experience/DT$driv_y_age
DT$xp_ratio_y[DT$driv_y_add_flg !=1 ] <- 0
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to convert factors from a data-frame to numeric using the commands
data[] <- lapply (data, function(x) as.numeric(as.character(x))
But it keeps asking me for more coding. What am I doing wrong?
The data-frame is named data and it consists of 50 rows and 2 columns. Will this command change every variable in numeric right? Or shall I do something else?
screenshot after using 'dput' at http://imgur.com/Sde9QSk.png
Shouldn't you add ) at the end of your code?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a data frame that has some empty entries. I set the
options(stringsAsFactors = FALSE)
so that I can change the empty cells. I then wrote the following code:
apply(my_data[,6:65],2, function(x) x[which(x=='')]<-0)
, hoping that it replaces all the empty cells with zeros. But it isn't working!
Note that my_data has 65 columns and columns 1:5 contain string.
Thanks in advance
No need to use apply, just use [<- with logical indexing
my_data[my_data==""] <- 0