如何将使用的定义值设置为数据框中的所有列
How to set a used defined value to all the columns in a data frame
我有一个
格式的数据框
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
我想将所有列的第 1 行的值设置为零(Time 列的第 1 行值除外)
我希望数据的格式是
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
请帮忙
使用 mtcars 作为我的数据框
mpg_first <- mtcars$mpg[1]
mtcars <- mtcars[-1,]
mtcars <- rbind(c(mpg_first,rep(0,ncol(mtcars)-1)),mtcars)
您可以使用索引将除第一列以外的所有列的第一行分配给 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
数据
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))
我有一个
格式的数据框 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
我想将所有列的第 1 行的值设置为零(Time 列的第 1 行值除外)
我希望数据的格式是
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
请帮忙
使用 mtcars 作为我的数据框
mpg_first <- mtcars$mpg[1]
mtcars <- mtcars[-1,]
mtcars <- rbind(c(mpg_first,rep(0,ncol(mtcars)-1)),mtcars)
您可以使用索引将除第一列以外的所有列的第一行分配给 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
数据
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))