Adding my logs (values) to a dataframe in R [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 4 years ago.
Improve this question
Okay so I'm making logs of some distance variables
- example;
loghospital=log(hospital_2015_distance, base=exp(1))
Works, i get values that i can run in a regression.
However for my LASSO regression it's better i specify a dataset.
So i want a dataframe of these logs (values).
Or better I want these logs (values) added to my existing dataframe called (data).
Any idea how this can be achieved? And if not, what else i should do to achieve the same?

To add it to your data.frame you can use $:
data$loghospital = log(hospital_2015_distance, base=exp(1))
Also you could use [[ or [ and probably should <- instead of = for assignment:
# Examples:
data[["loghospital"]] <- log(hospital_2015_distance, base=exp(1))
data["loghospital"] <- log(hospital_2015_distance, base=exp(1))
data[, "loghospital"] <- log(hospital_2015_distance, base=exp(1))

Related

Object not detected 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 4 years ago.
Improve this question
When I use correlation function in RstudioThis I get below error message:
The dataset that I have imported is show below and V1,V2 are given by default to the columns:
There are two ways:
1) Attaching the data:
attach(data1)
And then this code should work:
cor(V1, V2)
2) Using $ for accesing columns in a dataframe
cor(data1$V1, data1$V2)
So, if I got you correctly, you want to find correlation between variables V1 and V2 in the dataframe data1. To refer to the column in the dataframe a $ sign is used. Then your code will look like:
cor(data1$V1,data1$V2)
or, if you want, you can also use with() function, which would narrow down the namespace to particular dataframe data1:
with(data1, cor(V1, V2))

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?

rare error when running my function 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 6 years ago.
Improve this question
I have two function. One to train a classifier and one to predict test data. If I run the predict function step by step it works fine, however If I call the predict function I get an error. I can't know what is happening due to the code of the function has no errors compiled manually. I've upload the two functions and the data on Github.you can access here
modelFit=mdp(Class = dades[,1],data=dades[,-1],lambda = 1,info.pred = T)
predict.mdp(modelFit, dades[1:5,-1])
Error in D[row, i] : subscript out of bounds
Thank you for anything you can do to help
The reason I could see was that in D[row, i] , variable row was overshooting the number of rows in D.
row itself is derived from vec.new : for(row in vec.new)
This piece is the culprit :
start=dim(D)[1]
vec.new=(start+1):(start+dim(newdata)[1])
vec.new starts from nrow(D)+1 , so even the first element is actually just beyond the size of D .
You can insert cat(row) in code and see.
I guess you will have to think about what start should be.

Simple R function with variable number of arguments [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
I am working on a project converting a bunch of stata code to R to perform data cleaning, and one of the things I'm trying to do is to write a single R function that cleans all of my Yes/No variables that were previously coded as (Yes = 1, No = 2) to standard dummy variables.
The thing is that the number of variables that need to be cleaned by this function will constantly be changing. So my guess is that the function will need to take as its arguments (1) the dataset/dataframe with all the variables, and (2) the list of variables that need to be cleaned.
Any help on this would be greatly appreciated, as I'm pretty new to R.
Thanks!
You could try this:
example <- data.frame(sex=runif(10),q1=rep.int(c(1,2),5),q2=rep.int(c(2,1),5))
yesno <- function(data, variables) {
data.new <- data
data.new[,names(data) %in% variables] <- -data[,names(data) %in% variables]+2
return(data.new)
}
example
yesno(example, c("q1","q2"))
sapply(data, function(x) {-x+2})
data contains your columns of 1, 2. The anonymous functions turns all Yes/1 into 1, and No/2 into 0.

apply function doesn't work [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 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

Resources