简体   繁体   English

R:在将所有元素粘贴到单个字符串之前,最优雅的方法来清理数据框

[英]R: Most elegant way to sanitize data frame before pasting all elements to single string

I have a data frame similar to below: 我有一个类似于下面的数据框:

df.example <- data.frame(c(2:3, NA, "John"), c("Mary", NA, "", NA), c("Dennis", 4:6))

 2     Mary     Dennis
 3     John     4
 <NA>           5      # note the empty string
 John  <NA>     6      # "John" occurs twice

I have omitted the row and column names because they contain no useful information. 我省略了行名和列名,因为它们不包含有用的信息。

I would like to convert this to a 1D list, containing ALL values in the data frame, removing NA and empty string values. 我想将其转换为1D列表,包含数据框中的所有值,删除NA和空字符串值。 I would like to retain only unique values, and concatenate them so I can use them as an argument for a SQL query later in the code. 我想只保留unique值,并连接它们,以便我可以在代码中稍后将它们用作SQL查询的参数。

This is my current solution: 这是我目前的解决方案:

temp <- df.example[!is.na(df.example)]

 # "2" "3" "John" "Mary" "John" "" "Dennis" "4" "5" "6"     

temp <- unique(temp[temp != ""])

 # "2" "3" "John" "Mary" "Dennis" "4" "5" "6"     

output <- paste0("'",temp,"'", collapse = ",")

 #"'2','3','John','Mary','Dennis','4','5','6'"

Now I can pass this string to SQL. 现在我可以将此字符串传递给SQL。

This seems like way too many lines of code to do what I was expecting to be a one-liner. 这看起来像太多的代码行来做我期待的单行代码。 Is there a more elegant way? 有更优雅的方式吗?

I agree with Rich Scriven's comment. 我同意Rich Scriven的评论。 However, if you want a one-liner answer perhaps something like: 但是,如果你想要一个单行答案,或许类似于:

df.example <- data.frame(c(2:3, NA, "John"), c("Mary", NA, "", NA), c("Dennis", 4:6))
names(table(as.vector(t(df.example)),exclude=c("",NA)))
    # [1] "2"      "3"      "4"      "5"      "6"      "Dennis" "John"   "Mary"  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找具有所有唯一值的data.frame第一列的最优雅方法是什么? - what is the most elegant way to find the first column of a data.frame that has all unique values? 将几个空行添加到 R 中的数据框中的最优雅方法? - Most elegant ways to add a few empty rows into a data frame in R? 检查 R 中缺失数据模式的最优雅方法是什么? - what is the most elegant way to check for patterns of missing data in R? R:确定数据框中数值变量的优雅方法 - R: elegant way to determine numeric variables in a data frame 将 function 应用于 data.table 或 data.frame 中的多对列的最优雅方法是什么? - What is the most elegant way to apply a function to multiple pairs of columns in a data.table or data.frame? 使用不纯的 function 遍历数据帧的行的最优雅的方法是什么? - What is most elegant way to loop through rows of a data frame with an impure function? R-最快/最有效的方式来转换数据帧中列的数据? - R - Fastest / Most Efficient way to convert data of a column in a data frame? 在数据框中粘贴一系列列[R] - Pasting a range of columns in a data frame [R] 在 R 中提取和粘贴 data.frame 的变量 - Extracting and pasting variables of a data.frame in R 将R中数据框中的所有值转换为字符串 - Converting to string all values in a data frame in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM