This question already has answers here:
Update a Value in One Column Based on Criteria in Other Columns
(4 answers)
Closed 5 years ago.
I have a data set called NFL. I am trying to flag PlayType by "Sack", replace the NA in PlayerPosition with "QB", and then go back to normal. I can't figure out the code to make it happen. So far I have this which is wrong:
NFL$PlayerPosition[NFL$PlayType == "Sack"] <- "QB"
This works?
NFL[NFL$PlayType == "Sack",]$PlayerPosition <- "QB"
Is this what you are trying to do? It should work.
#Create dummy data
NFL <- data.frame(PlayType = c("A","B","C","Sack"),PlayerPosition = c(NA,NA,NA,NA))
#filter
NFL[NFL$PlayType == "Sack",]$PlayerPosition <- 'QA'
Related
This question already has answers here:
How to remove columns with same value in R
(4 answers)
Closed 2 years ago.
I have a really large dataset and I want to filter out some of the columns because it is the same data all throughout (ex: company name is all "Walmart"). I can go through and do these manually but I'm looking for a code to do it automatically.
I had in mind a function to subset based on if sum(unique(colnam)) == 1 but not sure how to get it to work. Thanks.
which(sapply(dat, function(col) length(unique(col)) == 1))
This question already has answers here:
Filter multiple values on a string column in dplyr
(6 answers)
Closed 3 years ago.
I have a large dataframe "Marks", containing marks each year from 2014/5-2017/8. I have separated the dataframe into 4 smaller ones, by year of completion using:
marks14 <-
Marks%>%
filter(YearOfCompletion == "2014/5")
marks15 <-
Marks%>%
filter(YearOfCompletion == "2015/6")
marks16 <-
Marks%>%
filter(YearOfCompletion == "2016/7")
marks17 <-
Marks%>%
filter(YearOfCompletion == "2017/8")
I am attempting now to separate the "2016/7" and "2017/8" marks in to one dataframe. I have tried to manipulate the filter function, but I'm unable to figure it out and I can't find the code for this in online cookbooks.
We can use %in% to filter a vector of dates with length greater than or equal to 1
library(dplyr)
Marks %>%
filter(YearOfCompletion %in% c("2016/7", "2016/8"))
This question already has answers here:
Using ifelse in R
(2 answers)
Closed 6 years ago.
I'm quite new to R, picked it up less than two weeks ago and was wondering why this didn't work. Basically what I'm trying to do is to loop through the new added column, compare a value of another column in the same row, and based on a condition, change the value in the column I'm looping.
myDataFrame["column2"] <- "a"
refValue = x
for(i in nrow(myDataFrame){
if(column1[i] >= refValue){
column2[i] <- "b"
}}
Tried to run it but the value doesn't change
View(MyDataFrame)
So myDataFrame is at th moment is
column1---------column 2
someValue------a
someValue------a
someValue------a
someValue------a
after it finished looping based on a condition which is the value of the corresponding row in column1, I want to change some of the 'a's to 'b's
No need to use loop for this. You can replace your code with
myDataFrame$column2 <- with(myDataFrame, ifelse(column1 >= x, "b", "a"))
This question already has answers here:
Select rows from a data frame based on values in a vector
(3 answers)
Closed 7 years ago.
let say i have dataset for State , year , week and location in csv format
i want retrieve all data for specific state for year 2014 from week 36 to week 53 as follow
i've already do this but still have another not need states, thnx
LH14 <- (function(){
x <- read.csv("LocationHotspot2014.csv")
x[,"State"] <- toupper(lh14[,"State"])
x[x$State=="California",],
x[x$Week== c(36:53) ,]
})()
Your code has several errors. This should run, assuming the csv file exists and has the appropriate columns.
LH14 <- function() {
x <- read.csv("LocationHotspot2014.csv")
x$State <- toupper(x$State)
x <- x[x$State == "CALIFORNIA" & x$Week %in% 36:53, ]
x
}
lh14 <- LH14()
edit: Replaced subset with indexing per Roman's suggestion.
This question already has answers here:
How to delete a row by reference in data.table?
(7 answers)
Closed 8 years ago.
I am trying to figure out how to remove a group of rows from a dataset by reference. For example, with this data set:
testset <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04","2013-10-05","2013-11-06")),
yr = c(2013,2013,2013,2013,2013),
mo = c(07,08,09,10,11),
da = c(02,03,04,05,06),
plant = LETTERS[1:5],
product = as.factor(letters[26:22]),
rating = runif(25))
I want to remove all rows where the product is "y". I have no idea how to go about this.
you can use either of the following commands -
testset_new <- subset(testset,product!="y")
or
testset_new <- testset[testset$product!="y",]