Skip to content
Prev 299469 / 398503 Next

How to replace a column in a data frame with another one with a different size

Your
On Sun, Jul 8, 2012 at 12:22 PM, Stathis Kamperis <ekamperi at gmail.com> wrote:
You're getting tripped up (again) by trying to sub-assign something
that's too small.

df is a rectangular array of data: on the RHS of that expression, you
are selecting out a subset of it of say 8 rows and telling R to
replace the 10-row V1 column with those 8 elements. This cannot be
done with the fixed rectangular structure and hence the error message.

What you want to do is something like this:

df[!is.na(df$V1), ]

Let's walk through that

df$V1 -- take the V1 column of df

is.na() -- get a logical vector saying where NAs are

!is.na() -- identify the rows where there _aren't_ NAs

df[ !is.na(), ] -- (the important one) take the rows of df (all
columns) where there aren't NAs

What you might be wanting to do is

df <- df[!is.na(df$V1), ]

This is much better than what you are trying to do (working on the
whole array at a time and trusting R to keep it all together than
trying to manipulate slices individually)

But even more idiomatic would be

complete.cases(df)

Take a look at some introductory material and try to wrap your head
around indexing rows and columns together again: it's a fantastic
paradigm and will be of much more use to you long run than trying to
work on individual columns for subsetting/data-cleaning.

Best,
Michael
Message-ID: <CAAmySGOuw_Tt717=1AvD-Y34jjPDoL7E1AAbaumZkiViXrd7Hg@mail.gmail.com>
In-Reply-To: <CACYCkUsBFy-nVmqy4odMYmFuEsyaE3pUcb2PonV6q1RXT=ki3g@mail.gmail.com>