We present a completely unbiased and controlled numerical method to solve quantum impurity problems in d-dimensional lattices. This approach is based on a canonical transformation, of the Lanczos form, where the complete lattice Hamiltonian is exactly mapped onto an equivalent one-dimensional system, in the same spirit as Wilson's numerical renormalization, and Haydock's recursion method. We introduce many-body interactions in the form of a Kondo or Anderson impurity and we solve the low-dimensional problem using the density matrix renormalization group. The technique is particularly suited to study systems that are inhomogeneous, and/or have a boundary. The resulting dimensional reduction translates into a reduction of the scaling of the entanglement entropy by a factor L d−1 , where L is the linear dimension of the original d-dimensional lattice. This allows one to calculate the ground state of a magnetic impurity attached to an L × L square lattice and an L × L × L cubic lattice with L up to 140 sites. We also study the localized edge states in graphene nanoribbons by attaching a magnetic impurity to the edge or the center of the system. For armchair metallic nanoribbons we find a slow decay of the spin correlations as a consequence of the delocalized metallic states. In the case of zigzag ribbons, the decay of the spin correlations depends on the position of the impurity. If the impurity is situated in the bulk of the ribbon, the decay is slow as in the metallic case. On the other hand, if the adatom is attached to the edge, the decay is fast, within few sites of the impurity, as a consequence of the localized edge states, and the short correlation length. The mapping can be combined with ab initio band structure calculations to model the system, and to understand correlation effects in quantum impurity problems starting from first principles.