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

半世苍凉 提交于 2019-12-13 03:35:32

问题


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 ...



来源:https://stackoverflow.com/questions/54776807/why-is-integer0-outputted-when-i-use-the-which-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!