我讀從工作目錄csv文件用下面的代碼:創建一個新的行並添加列總結
df <- read.csv("test1.csv", header = TRUE,skip =6, nrow =
length(count.fields("test1.csv")) - 12)
然後改變用下面的代碼中的列名:
colnames(df) = c("type","date","v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13","v14","v15","v16","v17","v18","v19","v20","v21","v22","v23","v24","total")
我的數據集的維度是365行x 24列,我試圖計算列(3:27)的總和並在數據框的底部用總和創建一個新行。
data.frame看起來像這樣:
如果我嘗試了一些樣品數據的測試如下正常工作:
x <- data.frame(x1 = c(3:8, 1:2), x2 = c(4:1, 2:5),x3 = c(3:8, 1:2), x4 = c(4:1, 2:5))
X [9,(2:3 )] < - apply(x,2,sum)
但是當我嘗試使用我正在使用的csv文件時,我使用的代碼如下所示:
x[366,(3:27)] <- apply(df, 2, sum)
但它給了這樣的錯誤: 「在FUN錯誤(下一頁末[,1] ...):無效的 '類型' 的說法(人物)」
誰能告訴我如何解決這個問題?
如果運行STR(DF),它告訴你,最後24列是類數字或整數?它看起來像一個或多個他們是一個字符類 – nzcoops 2012-03-30 12:20:25
嗨,如果我做str(df)它說最後的25 coloumns'num' – Achak 2012-03-30 13:05:57