Displaying 2 results from an estimated 2 matches for "ambertuil".
Did you mean:
amberti
2011 Dec 11
2
incomplete final line found warning
...wing as a UTF-8 encoded file named amberutil.r
as.factor.loop <- function(df, cols){
if (!is.null(df) && !is.null(cols) && length(cols) > 0)
{
for(col in cols)
{
df[[col]] <- as.factor(df[[col]])
}
}
df
}
And got this warning message,
> source('D:/ambertuil.r')
Warning message:
In readLines(file) : incomplete final line found on 'D:/ambertuil.r'
Can you help with this?
Regards,
Xiaobo Gu
2011 Dec 11
2
as.factor does not work inside function
...o write a function do cast columns of data frame as
factor in a loop, the source is :
as.factor.loop <- function(df, cols){
if (!is.null(df) && !is.null(cols) && length(cols) > 0)
{
for(col in cols)
{
df[[col]] <- as.factor(df[[col]])
}
}
}
source('D:/ambertuil.r')
x <- 1:5
y <- 2:6
df <- data.frame(x=x, y=y)
as.factor.loop(df, c("x"))
But after the function call, the df data frame does not change,
because
is.factor(df[["x]])
FALSE
But if I call this in R console directlly, it works
for(col in c("x","y")...