R - delete rows with more than x chars using which

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



R - delete rows with more than x chars using which



I have a dataframe with 7 variables. Some variables have more characters then they are supposed to have. To find the lines containing too many characters for one value I use this:


df <- df[-c(which(nchar(as.character(df$u)) > 5)), ]



So every line with more than 5 characters in df$u should be deleted. The problem is that this approach deletes everything. In this case there are no values in df$u that have more than 5 chars, so nothing should be deleted. If I change the line above to


df$u


df <- df[-c(which(nchar(as.character(df$u)) > 4)), ]



Two lines are deleted, which is correct as there are two occasions where df$u has more than 4 chars.


df$u



The Problem is that I can't figure out where the issue is. It worked just fine with hundreds of files and suddenly stopped working.



A small code example of df:


df


station date u v w temp dir
Balc 2017.12.25_0:0:0.005940 0.66 0.81 0.65 2.22 320.8
Balc 2017.12.25_0:0:0.106316 0.34 0.53 0.36 2.22 327.5
Balc 2017.12.25_0:0:0.205374 0.4456786 0.60 0.49 2.20 323.9
Balc 2017.12.25_0:0:0.306819 0.43 0.35 0.82 2.22 309.5





Just do df[nchar(df$u)<=5,] OR df[!(nchar(df$u)>5),] not need for which and as.character
– A. Suliman
Aug 8 at 14:41



df[nchar(df$u)<=5,]


df[!(nchar(df$u)>5),]


which


as.character





A reproducible example would be very helpful here
– CPak
Aug 8 at 14:45




1 Answer
1



The problem is in the indexing. If nothing has more than 5 values the conditions inside which returns all FALSE and which returns integer(0), and indexing the column with this return nothing.
Try with logical:


which


which


integer(0)


df[!(nchar(as.character(df$u)) > 5),]





Thanks! No idea why I didn't notice that.
– BallerNacken
Aug 8 at 14:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered