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?
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 1 year ago.
Improve this question
I need help how to compare 2 columns in r studio but only in the first 5 observations
code used first was
damgeByage<-table(test$Vehicle_Age,test$Vehicle_Damage)
damgeByage
which give me all 186 observation but only want the first 10 oobservation of both columns
I
We can subset the data with row index
table(test[1:10, c("Vehicle_Age", "Vehicle_Damage")])
akrun's answer works or you can also use:
table(test$Vehicle_Age[1:10],test$Vehicle_Damage[1:10])
the table is also subsettable if you just wanted the first 10 rows of the results:
damgeByage<-table(test$Vehicle_Age,test$Vehicle_Damage)
damgeByage[1:10,]
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
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))
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 working on a project in R (on TED_Talks data set). I have a data frame with one column called "tags" which contains a character like
"gaming,gender,sex,feminism,education,culture".
The problem is, the whole row is being read as a single character.
I want the output to be a vector containing separate words. eg:
"gaming","gender","sex","feminism","education","culture"
so I can do further analysis on tags.
You can simply do the following:
say your entry is in object a, and you want to allocate the final result to object b:
a <- "gaming,gender,sex,feminism,education,culture"
b <- unlist(strsplit(a, "[,]"))
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