打印一列的唯一值,紧跟在一个字符串句子旁边 [使用 paste()]

huangapple go评论59阅读模式
英文:

print unique values of a column next to a string sentence [using paste()]

问题

您想要的输出是:

"The following dates are present in the datetime column: 2023-04-17 13:20, 2023-04-19 16:13, 2023-04-19 16:46, 2023-04-18 09:23"

以下是代码部分的翻译:

df <- data.frame(datetime = c("2023-04-17 13:20", "2023-04-19 16:13", "2023-04-19 16:46", "2023-04-18 09:23"))

print(paste("The following dates are present in the datetime column: ", 
            unique(df$datetime), sep = ""))

希望这对您有所帮助。如果您需要进一步的帮助,请随时提问。

英文:

example code:

df &lt;- data.frame(datetime = c(&quot;2023-04-17 13:20&quot;, &quot;2023-04-19 16:13&quot;, &quot;2023-04-19 16:46&quot;, &quot;2023-04-18 09:23&quot;))

print(paste(&quot;The following dates are present in the datetime column: &quot;, 
              unique(df$datetime), sep = &quot;&quot;))

output I get:

[1] &quot;The following dates are present in the datetime column: 2023-04-17 13:20&quot;
[2] &quot;The following dates are present in the datetime column: 2023-04-19 16:13&quot;
[3] &quot;The following dates are present in the datetime column: 2023-04-19 16:46&quot;
[4] &quot;The following dates are present in the datetime column: 2023-04-18 09:23&quot;

Output I desire:

&quot;The following dates are present in the datetime column: 2023-04-17 13:20, 2023-04-19 16:13, 2023-04-19 16:46, 2023-04-18 09:23&quot;

I tried to create a vector of c(unique(df$datetime)), but that did not help either. Does anyone know a nice way of solving this?

答案1

得分: 2

你可以“双重”粘贴:

print(paste("以下日期出现在日期时间列中:", 
            paste(unique(df$datetime), collapse=", "), sep = ""))
            
[1] "以下日期出现在日期时间列中:2023-04-17 13:20, 2023-04-19 16:13, 2023-04-19 16:46, 2023-04-18 09:23"
英文:

you can "double" paste:

print(paste(&quot;The following dates are present in the datetime column: &quot;, 
            paste(unique(df$datetime),collapse=&quot;, &quot;), sep = &quot;&quot;))

[1] &quot;The following dates are present in the datetime column: 2023-04-17 13:20, 2023-04-19 16:13, 2023-04-19 16:46, 2023-04-18 09:23&quot;

huangapple
  • 本文由 发表于 2023年4月20日 04:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定