dpht05
dpht05

Reputation: 15

Why is R giving me NA values when there is data in the dataframe?

I have a dataframe with several columns and rows but to simplify and get the question across, I've created a dummy dataframe with 0 in it but the error is the same. I cannot see the issue as I've done this process before and never got an error.

dummy <- data.frame(matrix(0, ncol=1, nrow=140253))

dummy$Date <- as.Date.POSIXct(seq(ISOdate(1800,1,1),  by = "days", length.out = nrow(dummy)))
dummy$day <- as.numeric(format(dummy$Date, '%d'))
dummy$month <- as.numeric(format(dummy$Date, '%m'))
dummy$year <- as.numeric(format(dummy$Date, '%Y'))

unique.years <- unique(dummy$year)

for(i.year in 1:length(unique.years)){
  
  year.data <- dummy[dummy$year == unique.years[i.year],]
  if(isLeapYear(year.data$year[i.year])==FALSE){
    lpy <- c(0,0,29,2,unique.years[i.year])
    lyear.data <- insertRow(as.matrix(year.data), 60, lpy)
  }else{
    lyear.data <- dummy[dummy$year == unique.years[i.year],]
  }
}

This is what I want to achieve but going through the code it gave me error that said:

Error in if (isLeapYear(year.data$year[i.year]) == FALSE) { : 
  missing value where TRUE/FALSE needed

Then I realised that this was due to the fact that it was not reading all years of the dataframe, I checked this by only running the first part and printing the following, giving the list with NA from year 2164:

for(i.year in 1:length(unique.years)){
  
  year.data <- dummy[dummy$year == unique.years[i.year],]
  print((year.data$year[i.year]))
}
+   print((year.data$year[i.year]))}
[1] 1800
[1] 1801
[1] 1802
[1] 1803
[1] 1804
[1] 1805
[1] 1806
[1] 1807
[1] 1808
[1] 1809
[1] 1810
[1] 1811
[1] 1812
[1] 1813
[1] 1814
[1] 1815
[1] 1816
[1] 1817
[1] 1818
[1] 1819
[1] 1820
[1] 1821
[1] 1822
[1] 1823
[1] 1824
[1] 1825
[1] 1826
[1] 1827
[1] 1828
[1] 1829
[1] 1830
[1] 1831
[1] 1832
[1] 1833
[1] 1834
[1] 1835
[1] 1836
[1] 1837
[1] 1838
[1] 1839
[1] 1840
[1] 1841
[1] 1842
[1] 1843
[1] 1844
[1] 1845
[1] 1846
[1] 1847
[1] 1848
[1] 1849
[1] 1850
[1] 1851
[1] 1852
[1] 1853
[1] 1854
[1] 1855
[1] 1856
[1] 1857
[1] 1858
[1] 1859
[1] 1860
[1] 1861
[1] 1862
[1] 1863
[1] 1864
[1] 1865
[1] 1866
[1] 1867
[1] 1868
[1] 1869
[1] 1870
[1] 1871
[1] 1872
[1] 1873
[1] 1874
[1] 1875
[1] 1876
[1] 1877
[1] 1878
[1] 1879
[1] 1880
[1] 1881
[1] 1882
[1] 1883
[1] 1884
[1] 1885
[1] 1886
[1] 1887
[1] 1888
[1] 1889
[1] 1890
[1] 1891
[1] 1892
[1] 1893
[1] 1894
[1] 1895
[1] 1896
[1] 1897
[1] 1898
[1] 1899
[1] 1900
[1] 1901
[1] 1902
[1] 1903
[1] 1904
[1] 1905
[1] 1906
[1] 1907
[1] 1908
[1] 1909
[1] 1910
[1] 1911
[1] 1912
[1] 1913
[1] 1914
[1] 1915
[1] 1916
[1] 1917
[1] 1918
[1] 1919
[1] 1920
[1] 1921
[1] 1922
[1] 1923
[1] 1924
[1] 1925
[1] 1926
[1] 1927
[1] 1928
[1] 1929
[1] 1930
[1] 1931
[1] 1932
[1] 1933
[1] 1934
[1] 1935
[1] 1936
[1] 1937
[1] 1938
[1] 1939
[1] 1940
[1] 1941
[1] 1942
[1] 1943
[1] 1944
[1] 1945
[1] 1946
[1] 1947
[1] 1948
[1] 1949
[1] 1950
[1] 1951
[1] 1952
[1] 1953
[1] 1954
[1] 1955
[1] 1956
[1] 1957
[1] 1958
[1] 1959
[1] 1960
[1] 1961
[1] 1962
[1] 1963
[1] 1964
[1] 1965
[1] 1966
[1] 1967
[1] 1968
[1] 1969
[1] 1970
[1] 1971
[1] 1972
[1] 1973
[1] 1974
[1] 1975
[1] 1976
[1] 1977
[1] 1978
[1] 1979
[1] 1980
[1] 1981
[1] 1982
[1] 1983
[1] 1984
[1] 1985
[1] 1986
[1] 1987
[1] 1988
[1] 1989
[1] 1990
[1] 1991
[1] 1992
[1] 1993
[1] 1994
[1] 1995
[1] 1996
[1] 1997
[1] 1998
[1] 1999
[1] 2000
[1] 2001
[1] 2002
[1] 2003
[1] 2004
[1] 2005
[1] 2006
[1] 2007
[1] 2008
[1] 2009
[1] 2010
[1] 2011
[1] 2012
[1] 2013
[1] 2014
[1] 2015
[1] 2016
[1] 2017
[1] 2018
[1] 2019
[1] 2020
[1] 2021
[1] 2022
[1] 2023
[1] 2024
[1] 2025
[1] 2026
[1] 2027
[1] 2028
[1] 2029
[1] 2030
[1] 2031
[1] 2032
[1] 2033
[1] 2034
[1] 2035
[1] 2036
[1] 2037
[1] 2038
[1] 2039
[1] 2040
[1] 2041
[1] 2042
[1] 2043
[1] 2044
[1] 2045
[1] 2046
[1] 2047
[1] 2048
[1] 2049
[1] 2050
[1] 2051
[1] 2052
[1] 2053
[1] 2054
[1] 2055
[1] 2056
[1] 2057
[1] 2058
[1] 2059
[1] 2060
[1] 2061
[1] 2062
[1] 2063
[1] 2064
[1] 2065
[1] 2066
[1] 2067
[1] 2068
[1] 2069
[1] 2070
[1] 2071
[1] 2072
[1] 2073
[1] 2074
[1] 2075
[1] 2076
[1] 2077
[1] 2078
[1] 2079
[1] 2080
[1] 2081
[1] 2082
[1] 2083
[1] 2084
[1] 2085
[1] 2086
[1] 2087
[1] 2088
[1] 2089
[1] 2090
[1] 2091
[1] 2092
[1] 2093
[1] 2094
[1] 2095
[1] 2096
[1] 2097
[1] 2098
[1] 2099
[1] 2100
[1] 2101
[1] 2102
[1] 2103
[1] 2104
[1] 2105
[1] 2106
[1] 2107
[1] 2108
[1] 2109
[1] 2110
[1] 2111
[1] 2112
[1] 2113
[1] 2114
[1] 2115
[1] 2116
[1] 2117
[1] 2118
[1] 2119
[1] 2120
[1] 2121
[1] 2122
[1] 2123
[1] 2124
[1] 2125
[1] 2126
[1] 2127
[1] 2128
[1] 2129
[1] 2130
[1] 2131
[1] 2132
[1] 2133
[1] 2134
[1] 2135
[1] 2136
[1] 2137
[1] 2138
[1] 2139
[1] 2140
[1] 2141
[1] 2142
[1] 2143
[1] 2144
[1] 2145
[1] 2146
[1] 2147
[1] 2148
[1] 2149
[1] 2150
[1] 2151
[1] 2152
[1] 2153
[1] 2154
[1] 2155
[1] 2156
[1] 2157
[1] 2158
[1] 2159
[1] 2160
[1] 2161
[1] 2162
[1] 2163
[1] 2164
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA

Does anybody have any idea why this is. There are no days missing and I cannot figure out why it stops after 2164.

Thanks very much in advance.

Upvotes: 1

Views: 999

Answers (1)

Wiktor Gustafsson
Wiktor Gustafsson

Reputation: 182

It stops after 2164 because 2165 is the 366th year. This because you are taking the i.yearth index of year.data, but each year.data is 365 rows so when you reach i.year == 366 you are out of bounds and will get NA back.

I'm guessing you just want to check if the current year is a leap year? Well, all the rows will have the same year value in the year.data df so just change your if statement to

if (!isLeapYear(unique.years[i.year]))

and that should fix it. Also note that !condition is preferred over condition == FALSE.

Upvotes: 1

Related Questions