0
所以我想通過使用for循環在數據框的幾列上操作一個函數。R錯誤:參數意味着不同的行數
z <- function(x) gsub("[^\\.\\d]", "", x, perl = TRUE)
data <- cbind(data[1:2], for(i in seq(3, 9)) {y(data[[i]])})
我一直運行到錯誤作爲主題
arguments imply differing number of rows
在我的所有列的行數相同提到。
我試着用lapply做這件事,但它雖然起作用,但它將我應用函數的列類型轉換爲因子。這些列是數字值,但最初從文件中讀取爲字符(它們存儲爲這樣)。所以當我嘗試使用lapply後轉換爲數字,我得到的數量級別作爲輸出(比如,1,2,3 ...)
任何建議,使用for循環或lapply都是受歡迎的。提前致謝。
> dput(head(data,3))
structure(list(MCF.Channel.Grouping = structure(c(6L, 6L, 6L), .Label = c("(Other)",
"Direct", "Display", "Email", "Organic Search", "Paid Search",
"Referral", "Social Network"), class = "factor"), Device.Category = structure(c(2L,
1L, 3L), .Label = c("desktop", "mobile", "tablet"), class = "factor"),
Spend = c("A$503,172.17", "A$375,940.43", "A$92,560.94"),
Clicks = c("1,545,416", "1,037,740", "291,314"), Impressions = c("7,328,657",
"3,787,612", "1,178,508"), Data.Driven.Conversions = c("1,697,814.32",
"1,540,810.43", "430,738.63"), Data.Driven.CPA = c("A$0.30",
"A$0.24", "A$0.21"), Data.Driven.Conversion.Value = c("A$12,815,842.66",
"A$13,883,073.58", "A$3,804,800.15"), Data.Driven.ROAS = c("2547.01%",
"3692.89%", "4110.59%")), .Names = c("MCF.Channel.Grouping",
"Device.Category", "Spend", "Clicks", "Impressions", "Data.Driven.Conversions",
"Data.Driven.CPA", "Data.Driven.Conversion.Value", "Data.Driven.ROAS"
), row.names = c(NA, 3L), class = "data.frame")
爲什麼這個data.table/dplyr相關? – Arun
我建議你從工作區發佈對象數據的'dput' – jangorecki
對不起,刪除這些標籤。我在同一個窗口中輸入了另一個問題,與dplr相關,但後來我發現了這個問題。 –