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 have a dataset named SPT which has one column which consist 4 unique values-A,B,C,D, Now I have to create one more column which consist of 1 if first column had A, B otherwise 0.How to do it using ifelse command
Using ifelse:
SPT$new <- ifelse(SPT$col %in% c('A','B'),1,0)
Where new must be changed by the name of your new variable, and col by the name of the column that is storing the 4 letters
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 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 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 6 years ago.
Improve this question
I have the following df
Case Points
A 2
B 3
C 8
I would the like to fetch the Points of a certain row. So lets say I have a list with rownumbers 2 and 3
rownumbers <- c(2,3)
Then I would like to the fetch the numbers 3 and 8. Could anybody tell me how to do this?
Try:
mydata[rownumbers,"Points"]
This answered in comments so I made it as community wiki