我正在使用的数据集有两个和三个字的变量名,中间有空格。
> names(final.data1)
## [1] "Vehicle ID" "Frame ID" "Total Frames"
## [4] "Global Time" "Local X" "Local Y"
## [7] "Global X" "Global Y" "Vehicle Length"
## [10] "Vehicle width" "Vehicle class" "Vehicle velocity"
## [13] "Vehicle acceleration" "Lane" "Preceding Vehicle ID"
## [16] "Following Vehicle ID" "Spacing" "Headway"
## [19] "svel" "sacc" "PrecVehClass"
我没有更改名称并继续按原样进行分析。当我使用transform
plyr
个包时,我得到了2个列,其中包含一些包含相同数字但名称不同的变量。
# Determining when the lane change occured
final.data1 <- ddply(final.data1, c("`Vehicle class`", "`Vehicle ID`"), transform,
lane.change = c(NA, ifelse(diff(Lane) != 0, "yes", ".")))
ordr <- with(final.data1, order(`Vehicle ID`))
final.data1 <- final.data1[ordr, ] #sort in ascending order by vehicle ID
names(final.data1)
## [1] "Vehicle class" "Vehicle ID" "Vehicle.ID"
## [4] "Frame.ID" "Total.Frames" "Global.Time"
## [7] "Local.X" "Local.Y" "Global.X"
## [10] "Global.Y" "Vehicle.Length" "Vehicle.width"
## [13] "Vehicle.class" "Vehicle.velocity" "Vehicle.acceleration"
## [16] "Lane" "Preceding.Vehicle.ID" "Following.Vehicle.ID"
## [19] "Spacing" "Headway" "svel"
## [22] "sacc" "PrecVehClass" "lane.change"
请参阅Vehicle ID
和Vehicle.ID
?为什么会发生这种情况?如果我不想更改原始变量名称,我该如何防止这种情况?
答案 0 :(得分:1)
问题实际上是transform
,这是&#34;检查&#34;变量名。如果您在check.names = FALSE
来电transform
之后添加ddply
,我认为您会得到您想要的内容。以下是玩具数据集的示例。
dat = data.frame("Vehicle class" = rep(c("a", "b"), each = 4),
"Vehicle ID" = rep(c("c","d"), times = 4),
Lane = sample(0:3, 8, replace = TRUE),
"Vehicle Length" = runif(8, 0, 100), check.names = FALSE)
require(plyr)
# Using transform changes names in output data.frame
ddply(dat, .(`Vehicle class`, `Vehicle ID`), transform,
lane.change = c(NA, ifelse(diff(Lane) != 0, "yes", ".")))
# Adding check.names = FALSE to change this behavior
ddply(dat, .(`Vehicle class`, `Vehicle ID`), transform, check.names = FALSE,
lane.change = c(NA, ifelse(diff(Lane) != 0, "yes", ".")))