0

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

Abhishek
  • 95
  • 1
  • 6

2 Answers2

0

Using mtcars as my dataframe

mpg_first <- mtcars$mpg[1]
mtcars <- mtcars[-1,]
mtcars <- rbind(c(mpg_first,rep(0,ncol(mtcars)-1)),mtcars)
Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83
0

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))
akrun
  • 874,273
  • 37
  • 540
  • 662