plyr中的转换功能是否会更改变量名称?

乌迈尔·杜拉尼

我正在使用的数据集有两个和三个词的变量名,两者之间有空格。

> 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"

我没有更改名称,而是继续进行分析。当我用transformplyr包,我有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 IDVehicle.ID为什么会发生这种情况?如果我不想更改原始变量名,该如何防止呢?

奥史密斯

问题实际上出在transform,它在“检查”变量名。如果您通话check.names = FALSE添加我想您会得到想要的。这是一个带有玩具数据集的示例。transformddply

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", ".")))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章