Cláudio Siva
Cláudio Siva

Reputation: 522

how to replace values in multiple rows in a dataframe in r?

This is how my data looks like:

dput(head(CORt_r100, 5))
structure(list(rDate = structure(c(1438019100, 1438019400, 1438019700, 
1438020000, 1438020300), class = c("POSIXct", "POSIXt"), tzone = ""), 
    I630 = c(0.536, 0.506, 0.419, 0.456, 0.427), I800 = c(0.414, 
    0.388, 0.339, 0.351, 0.331), I532 = c(0.547, 0.534, 0.463, 
    0.488, 0.464), I570 = c(0.522, 0.508, 0.467, 0.468, 0.445
    ), WR630 = c(0.0127, 0.0573, 0.0083, 0.0057, 0.0053), WR800 = c(0.0144, 
    0.0506, 0.0249, 0.0163, 0.0159), WR532 = c(0.0139, 0.0394, 
    0.006, 0.005, 0.0049), WR570 = c(0.0176, 0.0379, 0.0094, 
    0.0054, 0.0049), NR630 = c(0.006, 0.034, 0.006, 0.004, 0.004
    ), NR800 = c(0.007, 0.04, 0.019, 0.02, 0.019), NR532 = c(0.007, 
    0.072, 0.01, 0.007, 0.007), NR570 = c(0.009, 0.077, 0.008, 
    0.007, 0.007), ER630 = c(0.0351, 0.0746, 0.0116, 0.0055, 
    0.0052), ER800 = c(0.0278, 0.0596, 0.03, 0.0324, 0.0303), 
    ER532 = c(0.04, 0.085, 0.013, 0.008, 0.008), ER570 = c(0.034, 
    0.083, 0.013, 0.009, 0.008), PotRad = c(NA, NA, NA, 256.497787465489, 
    NA), Rg = c(NA, NA, NA, 230.782, NA), PotRad.f = c(256.497787465489, 
    256.497787465489, 256.497787465489, 256.497787465489, 235.488150060979
    ), Rg.f = c(230.782, 230.782, 230.782, 230.782, 214.260333333333
    ), REFN532 = c(0.0127970749542962, 0.134831460674157, 0.0215982721382289, 
    0.014344262295082, 0.0150862068965517), REFN570 = c(0.0172413793103448, 
    0.151574803149606, 0.0171306209850107, 0.014957264957265, 
    0.0157303370786517), REFN630 = c(0.0111940298507463, 0.0671936758893281, 
    0.0143198090692124, 0.0087719298245614, 0.00936768149882904
    ), REFN800 = c(0.0169082125603865, 0.103092783505155, 0.056047197640118, 
    0.056980056980057, 0.0574018126888217), REFW532 = c(0.0254113345521024, 
    0.0737827715355805, 0.0129589632829374, 0.0102459016393443, 
    0.0105603448275862), REFW570 = c(0.0337164750957854, 0.0746062992125984, 
    0.0201284796573876, 0.0115384615384615, 0.0110112359550562
    ), REFW630 = c(0.0236940298507463, 0.113241106719368, 0.0198090692124105, 
    0.0125, 0.0124121779859485), REFW800 = c(0.0347826086956522, 
    0.130412371134021, 0.0734513274336283, 0.0464387464387464, 
    0.048036253776435), REFE532 = c(0.0731261425959781, 0.159176029962547, 
    0.0280777537796976, 0.0163934426229508, 0.0172413793103448
    ), REFE570 = c(0.0651340996168582, 0.163385826771654, 0.0278372591006424, 
    0.0192307692307692, 0.0179775280898876), REFE630 = c(0.0654850746268657, 
    0.147430830039526, 0.0276849642004773, 0.0120614035087719, 
    0.0121779859484778), REFE800 = c(0.0671497584541063, 0.15360824742268, 
    0.0884955752212389, 0.0923076923076923, 0.0915407854984894
    ), Date = structure(c(16643, 16643, 16643, 16643, 16643), class = "Date")), row.names = c(NA, 
5L), class = "data.frame")

My question is rather simple but I couldn't find a solution: I found out that a lot of rows in my dataframe (nº of rows: 222, 224, 510, 512, 2238, 2239, 2240, 2526, 2527, 2528, 2814, 2815, 2816, 3103, 4255, 5980, 5981, 5982, 5983) does not fulfill one condition. Instead of erasing the rows, I have to replace all the values (which are numeric) in each row with NA, but the rDate has to be maintained. I tried simply:

CORt_r100[222, 224, 510, 512, 2238, 2239, 2240, 2526, 2527, 2528, 2814, 2815, 2816, 3103, 4255, 5980, 5981, 5982, 5983, ] <- NA

but without success. I tried other variations of this code but the dataframe maintains the same. Any idea how I could do this operation? Thank you in advance.

Upvotes: 1

Views: 1052

Answers (2)

Jeffrey Evans
Jeffrey Evans

Reputation: 2397

You are almost there, you have to concatenate your index eg., x[c(222, 224, 510),] <- NA. However, this will not do what you are after as it will assign NA's to the entire row, including the date column.

Here is a small subset of your data

x <- data.frame(
rDate = structure(c(1438019100, 1438019400, 1438019700, 
1438020000, 1438020300), class = c("POSIXct", "POSIXt"), tzone = ""),
I630 = c(0.536, 0.506, 0.419, 0.456, 0.427), 
I800 = c(0.414, 0.388, 0.339, 0.351, 0.331), 
I532 = c(0.547, 0.534, 0.463, 0.488, 0.464),
ER570 = c(0.034, 0.083, 0.013, 0.009, 0.008), 
PotRad = c(NA, NA, NA, 256.497787465489, NA), 
Rg = c(NA, NA, NA, 230.782, NA))    

We can assign NA's to row's 1, 2 and 5 and specify which columns in the second bracket.

x[c(1,2,5),][2:ncol(x)] <- NA

You can also use which in interesting ways to say, find a condition and then perform a similar NA replace (to run example recreate x as in the previous example we replaced rows with NA's). In this case we are looking for rows where ER570 < 0.01 and I630 < 0.5.

( idx <- which(x$ER570 < 0.01 & x$I630 < 0.5) )
  x[idx,][2:ncol(x)] <- NA

Upvotes: 1

akrun
akrun

Reputation: 886938

We need to concatenate the row index to a vector along with specify the columns not needed using setdiff i.e. setdiff(names(CORt_r100), "Date") as column names

CORt_r100[c(222, 224, 510, 512, 2238, 2239, 2240, 2526, 2527, 2528, 2814, 2815, 2816, 3103, 4255, 5980, 5981, 5982, 5983), setdiff(names(CORt_r100), "Date") ] <- NA

Upvotes: 1

Related Questions