r - I cannot subset my data frame using column values -


i want subset data frame based on specific values of a column. code :

data <- read.csv("file.csv") data1 <- data[ ,week_no < 2]  write.csv(data1, "joda.csv",row.names=false) 

but r gives me error :

error in `[.data.frame`(data, , week_no < 2) : object 'week_no' not found  

the column exists,but don't know why receive error. thankful if can help.

there 3 errors in code, follows

use mtcars dataset example; subset dataset condition disp < 200

data(mtcars) 

first index position wrong c8h10n4o2 noted in comments). when subsetting column wanting select rows match constraint. adding constraint in row position data[row, col]

mtcars[mtcars$disp < 200, ] 

you need tell r disp is. will give error there no object called disp in global environment. seems presistent mistake making.

mtcars[disp < 200, ] 

error in [.data.frame(mtcars, disp < 200, ) : object 'disp' not found

so need pass dataframe name in constraint mtcars$disp < 200

as user227710 noted in comment, r case-sensitive, if incorrectly spelled variable passed, not found no rows returned

mtcars[mtcars$disp < 200, ] 

you misspelled column name. guess should week_no, capital 'w' , capital 'o' instead of zero.

if error remains, check out names(data) see correct spelling.


Comments

Popular posts from this blog

What Are The Best Universities In The World?

Hard vs. Soft Water: What's The Difference?

The Ten Most Livable Cities In The World