-->

Why is integer(0) outputted when I use the which()

2019-08-20 10:55发布

问题:

I am trying to determine which variables in my V1 column have values in the V5 column that are in the range of 95-105 and also have values in the V6 column that are in the 7-13 range. I am using the which function and attempting to store the names of the variables in V1 under the variable x but I keep getting the output integer(0) or character(0) and I'm not sure what that means. An image of my code is attached below.

回答1:

integer(0) means there are no elements of your data frame that satisfy the conditions. (You could try

with(df, any(95 <= V5 & V5 <= 105 & 
             13 <= V6 & V6 <= 17))

(edited on the basis of @H1's comment, to match your description rather than your code); rearranging slightly to approximate the A < B < C syntax that R's parser can't handle ...)

You should probably check str(df) and/or summary(df) (or sapply(df, class)) to make sure that your data frame has really been read in as intended (or use dplyr::read_csv(), which prints information about the classes inferred from the data set. In particular, any typos in your data that make an entry not be a valid number (extra decimal point, missing value such as "?" not recognized as missing, etc.) will make R interpret the entire column as a character (since you've set stringsAsFactors=FALSE) rather than a numeric variable.

If you want to force columns 2-14 to numeric, you can use df[-1] <- lapply(df[-1], as.numeric) however, it would be better practice to find and fix any problems upstream ...



标签: r which