Reputation: 33
The title might be misleading but I have the scenario here:
half_paper <- lapply(data_set[,-1], function(x) x[x==0]<-0.5)
This line is supposed to substitute 0 for 0.5 in all of the columns except the first one.
Then I want to take half_paper and put it into here where it would rank all of the columns except the first one in order.:
prestige_paper <-apply(half_paper[,-1],2,rank)
But I get an error and I think that I need to somehow make half_paper into a data set like data_set.
Thanks for all of your help
Upvotes: 1
Views: 39
Reputation: 71
Your main issue 'This line is supposed to substitute 0
for 0.5
in all of the columns except the first one' can be remedied by placing another line in your anonymous function. The gets operator <-
returns the value of whatever is on the right hand side, so your lapply
was returning a value of 0.5
for each column. To remedy this, another line can be added to the function that returns the modified vector.
It's also worth noting that lapply
returns a list. apply
was substituted in for lapply in this case for consistency, but plyr::ddply
may suit this specific need better.
half_mtcars <- apply(mtcars[, -1], 2, function(x) {x[x == 0] <- .5;return(x)})
prestige_mtcars_tail <- apply(half_mtcars, 2, rank)
prestige_mtcars <- cbind(mtcars[,1, drop = F], prestige_mtcars_tail)
Upvotes: 1