[英]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.