user1489975
user1489975

Reputation: 1921

Replacing row data in column

The data set i am using treat missing value as -99 and now I need to replace all these -99 to the number in same row but different column.

Here is the example

V1   V2    V3  V4       V5            V6        V7
1   1958    3 1958.208 315.71       315.71    314.62    -1
2   1958    4 1958.292 317.45       317.45    315.29    -1
3   1958    5 1958.375 317.50       317.50    314.71    -1
4   1958    6 1958.458 **-99.99**   *317.10*  314.85    -1

I want to replace (V5, 4) with (V6, 4).

There are several missing data in V5 and we want to replace with the same row in V6.

How to achieve this?

Upvotes: 0

Views: 171

Answers (1)

mnel
mnel

Reputation: 115485

It would be more sensible to specify the NA values when you read in your data.

You can do this by specifying the na.string argument in read.table() (or some variant of such).

With your specific data, it is unclear whether you have a defined scheme (as in always replace with the value in the next column), but for replacing 'missing' values in column V5 with their respective values in V6 and assuming your data is called DF

 DF <- within(DF, V5 <- replace(V5, V5 == -99.99, V6[V5== -99.99])

would work.

If you have correctly specified the -99.99 as missing values (NA) then

DF <- within(DF, V5 <- replace(V5, is.na(V5), V6[is.na(V5)])

Upvotes: 2

Related Questions