apply function doesn't work [closed] - r

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

Related

How to retrieve a value in a dataframe and use it as variable in R? [closed]

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

Re-labeling columns in R [closed]

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 need to replace number values with something that makes more sense to the reader.
Value “-0.95197” represents “18-24” age group in the given table.
What would be the best way to go about this for all columns?
Your question is a little unclear. Are you trying to rename the header of each column in a data.frame? If so, try this, where df is the name of your data.frame. You need one piece of text for each column.
names(df) <- c("your", "header", "names", "go", "here")
If this is not what you want, then you need to provide more info.

Dataframe convert factors to numerical error [closed]

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?

Can I group * things from this data? [closed]

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 got data with ASCII form.
I ran it with R, and these data have * marked when it is under other condition.
enter image description here
V1, V2, V3, V4, V5 don't mean anything different. All that matters is to classify between *-ed things.
I tried c(V1,V2,V3,V4,V5) but it returns only the levels.
I have no idea. Help me with it.
Question. Can I specify *-ed things via some code?
Is there a way to make these columned things in one data?
Select the values marked with *. I guess these values come with the symbol from the original file, right?
In this case use:
position <- grep('\\*', as.matrix(distress[]))
selectedValues <- as.matrix(distress[])[position]
numericValues <- as.numeric(gsub('\*', '', selectedValues))

Getting the Mean by a Factor [closed]

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

Resources