這可能比上面的答案稍簡單一些。
#where your dataframe = df
df.name1 <- as.character (df.name1)
df.name2 <- as.character (df.name2)
我需要在工作中一直這樣做,因爲數據太混亂了。我已經能夠使用StringsAsFactors = FALSE進行導入,但是在最新版本的r中,我在read.csv中收到錯誤。理想情況下,我會很快弄清楚......與此同時,我一直在做這個快速有效的方法。 它將舊變量foo作爲因子類型,並將其轉換爲新變量fooChar,它是字符類型。我通常通過將新變量命名爲舊變量來進行原位操作,但您可能希望在用它來替代值之前先使用它。
#Convert from Factor to Char
#Data frame named data
#Old Variable named foo, factor type
#New Variable named fooChar, character type
data$fooChar <-as.character(data$foo)
#confirm the data looks the same:
table (data$fooChar)
#confirm structure of new variable
str(data)
您是如何創建數據框的?如果您正在讀取分隔文件,您也可以在導入過程中執行此操作。 – beroe