rquantitative-financehft

How to set a used defined value to all the columns in a data frame


I have a data frame of the format

                Time Ask Bid Trade Ask_Size Bid_Size Trade_Size 
2016-11-01 09:00:12  NA 901    NA       NA      100         NA  
2016-11-01 09:00:21  NA  NA   950       NA       NA          5  
2016-11-01 09:00:21  NA 950    NA       NA        5         NA 

I want to set the values of 1st row for all the column to zero (except the 1st row value of the column Time)

I want the data to be of the format

                Time Ask Bid Trade Ask_Size Bid_Size Trade_Size 
2016-11-01 09:00:12  0   0     0         0       0           0  
2016-11-01 09:00:21  NA  NA   950       NA       NA          5  
2016-11-01 09:00:21  NA 950    NA       NA        5         NA 

Please help


Solution

  • You can use the indexing to assign the first row for all columns except the first column to 0

    df1[1, -1] <- 0
    df1
    #                 Time Ask Bid Trade Ask_Size Bid_Size Trade_Size
    #1 2016-11-01 09:00:12   0   0     0        0        0          0
    #2 2016-11-01 09:00:21  NA  NA   950       NA       NA          5
    #3 2016-11-01 09:00:21  NA 950    NA       NA        5         NA
    

    data

    df1 <- structure(list(Time = c("2016-11-01 09:00:12", "2016-11-01 09:00:21", 
    "2016-11-01 09:00:21"), Ask = c(NA, NA, NA), Bid = c(901L, NA, 
    950L), Trade = c(NA, 950L, NA), Ask_Size = c(NA, NA, NA), Bid_Size = c(100L, 
    NA, 5L), Trade_Size = c(NA, 5L, NA)), .Names = c("Time", "Ask", 
    "Bid", "Trade", "Ask_Size", "Bid_Size", "Trade_Size"), class = "data.frame",
     row.names = c(NA, -3L))