I have created a data frame from a SQL query using the RODBC package. I have a separate single column data frame in which each rows contains a name of a column in the main data set that I would like to convert to a factor.
This is the logic though the syntax is obviously wrong.
for(c in 1:length(df.ToFactor$IV)-0) {
VarToFactor<- as.character(df.ToFactor$IV[c])
df.dataset[VarToFactor]<-factor(df.HRV[VarToFactor])
}
Any help would be appreciated.
The subtraction of zero from a vector of integers doesn't make much sense. I suspect you may wnat to use the [[<var-name>]]
construction:
for(c in 1:length(df.ToFactor$IV) ) {
VarToFactor<- as.character(df.ToFactor[["IV"]][c])
df.dataset[[VarToFactor]]<-factor(df.HRV[[VarToFactor]])
}
I also changed the "$" operation to the equivalent "[[" operation just because it's safer in programming, although I think in a for-loop at an interactive session it would not cause problems. In addition to the ?Extract page where the detail of "[[" and "[" are described one can find useful information in the "R Inferno" by Patrick Burns. This particular area is covered on p 52.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.