R beginner here. I have a data.frame that contains information on trotting horses (their wins, earnings, time records and such). I have a subsetted data.frame organised in a way that every row contains information for every specific year the horse competed. I have a variable called Competition.age that states what age the horses were each year they competed.
I'm writing down my summary statistics stratified by age and sex of the horse using both the summary()
function and describe()
from package psych
. For example:
summary(Data_year[Data_year$Competition.age>="3"&
Data_year$Competition.age<="6"& Data_year$Sex=="Mare", ])
This works perfectly fine. But when I try to get a range between 7 and 10 years (instead of 3 and 6), it only returns NA's. The str()
function with this line of code returns a blank list of variables-for some reason it won't read the data.
I've even created separate subsetted data.frames with only these years (7, 8, 9 and 10 respectively) and there are no problems with those, individually. I created subsetted data frames with ranges 7-8, 7-9 and they were fine! But 7-10 created an empty data.frame.
Any help will be greatly appreciated!!
答案 0 :(得分:0)
在您的评论中,您写的Data_year$Competition.age
是一个整数。现在有以下事实:"7"
不是数字。如果将数值与非数字值(例如字符)进行比较,则将数值强制转换为字符,并对字符进行比较(字母顺序)。按字母顺序"3"
更大(后)"10"
见这个例子:
age <- 1:15
sort(as.character(age))
您需要Data_year$Competition.age>=3
和Data_year$Competition.age<=6
等等。