我有一個資料框
a = c("A","B","C")
b = c(12,13,14)
c = ("Great","OK","Bad")
df = data.frame(a,b,c)
我想用所有列的預期輸出列印出每一行:
A is 12 mins and it is Great
B is 13 mins and it is OK
C is 14 mins and it is Bad
我嘗試使用cat或 paste0 但它不能像我想要的那樣作業
uj5u.com熱心網友回復:
joe NG,當您可以使用單獨的向量來獲得所需的輸出時,我不建議您創建資料框,但是可能有更多的方法來獲得所需的輸出。
a = c("A","B","C")
b = c(12,13,14)
c = c("Great","OK","Bad")
# create loop
d <- c(1:3)
# loop script to print output
for (x in 1:3){
print(paste0(a[x]," is ",b[x]," mins and it is ",c[x]))}
uj5u.com熱心網友回復:
你可以使用sprintf-
with(df, sprintf('%s is %d mins and it is %s', a, b, c))
#[1] "A is 12 mins and it is Great" "B is 13 mins and it is OK"
#[3] "C is 14 mins and it is Bad"
如果你需要這個顯示的目的,在添加一個新行每行paste0有cat。
cat(with(df, paste0(sprintf('%s is %d mins and it is %s', a, b, c), collapse = '\n')))
#A is 12 mins and it is Great
#B is 13 mins and it is OK
#C is 14 mins and it is Bad
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340024.html
上一篇:使用htmltools::withTags()更改R中資料表的頁腳
下一篇:保存(并打開)一個png圖串列
