在 data-frame/data.table 中使用换行符连接和粘贴两列
Concatenating and pasting two columns using line breaks within data-frame/data.table
这是我的数据框的样子
library(data.table)
dt <- fread('
Batch Score Type Description
A 1 fruit apple
A 2 beverage pepsi
A 3 food rice
B 1 beverage coke
B 2 fruit banana
C 1 food butter
D 1 food bread
')
一旦我确定了特定批次得分最高的行,我想连接 Type
和 Description
并将它们一直粘贴(所有元素从第一行到得分最高的最后一行)在类型和描述之间使用 换行符 和 间隙 。粘贴到该批次得分最高的行,如下所示:
Batch Score(Max) Description2
A 3 fruit apple
beverage pepsi
food rice
B 2 beverage coke
fruit banana
C 1 food butter
D 1 food bread
该批次得分最高的行也恰好是我的数据框中该批次的最后一行。我试过:
dt[, .(MaxScore = max(Score),
Description2 = cat(paste(Type, Description), sep="\n")),by = .(Batch)]
目标是在一列(Description2)和一行中显示所有信息 batch.Your 非常感谢帮助!
注意:这并没有完全回答 OP 的问题。
我做了类似的事情(但不完全一样):
print(x=dt[,{
mx = max(Score)
subdat = .SD[, !"Score", with=FALSE]
newrow0 = lapply(subdat, function(x) as("",class(x)))
newrow = newrow0
newrow[length(newrow)] = paste0(" Max Score: ", mx)
rbind(
subdat,
newrow,
newrow0
)
}, by=Batch], nrows=Inf, row.names=FALSE)
这给出了
Batch Type Description
A fruit apple
A beverage pepsi
A food rice
A Max Score: 3
A
B beverage coke
B fruit banana
B Max Score: 2
B
C food butter
C Max Score: 1
C
D food bread
D Max Score: 1
D
我只是用它来查看控制台中的数据。我将组的元数据放在一行而不是一列中,因为我有足够的列几乎已经可以跨越屏幕了。
请注意,as("", class(x))
非常不可靠(例如,不适用于 Date
class)。可能有必要强制整个 table 先串起来。
要将多列打印为一列,请使用 sprintf
:
print(x=dt[,{
mx = max(Score)
subdat = .(
Description = as.character(Description),
Type = as.character(Type)
)
.(mx, sprintf("%10s %15s", subdat$Type, subdat$Description))
}, by=Batch], nrows=Inf, row.names=FALSE)
Batch V1 V2
A 3 fruit apple
A 3 beverage pepsi
A 3 food rice
B 2 beverage coke
B 2 fruit banana
C 1 food butter
D 1 food bread
这是非常手动的,但从 sapply(dt, function(x) max(nchar(x)))
.
开始,如何以编程方式完成它应该是显而易见的
这是我的数据框的样子
library(data.table)
dt <- fread('
Batch Score Type Description
A 1 fruit apple
A 2 beverage pepsi
A 3 food rice
B 1 beverage coke
B 2 fruit banana
C 1 food butter
D 1 food bread
')
一旦我确定了特定批次得分最高的行,我想连接 Type
和 Description
并将它们一直粘贴(所有元素从第一行到得分最高的最后一行)在类型和描述之间使用 换行符 和 间隙 。粘贴到该批次得分最高的行,如下所示:
Batch Score(Max) Description2
A 3 fruit apple
beverage pepsi
food rice
B 2 beverage coke
fruit banana
C 1 food butter
D 1 food bread
该批次得分最高的行也恰好是我的数据框中该批次的最后一行。我试过:
dt[, .(MaxScore = max(Score),
Description2 = cat(paste(Type, Description), sep="\n")),by = .(Batch)]
目标是在一列(Description2)和一行中显示所有信息 batch.Your 非常感谢帮助!
注意:这并没有完全回答 OP 的问题。
我做了类似的事情(但不完全一样):
print(x=dt[,{
mx = max(Score)
subdat = .SD[, !"Score", with=FALSE]
newrow0 = lapply(subdat, function(x) as("",class(x)))
newrow = newrow0
newrow[length(newrow)] = paste0(" Max Score: ", mx)
rbind(
subdat,
newrow,
newrow0
)
}, by=Batch], nrows=Inf, row.names=FALSE)
这给出了
Batch Type Description
A fruit apple
A beverage pepsi
A food rice
A Max Score: 3
A
B beverage coke
B fruit banana
B Max Score: 2
B
C food butter
C Max Score: 1
C
D food bread
D Max Score: 1
D
我只是用它来查看控制台中的数据。我将组的元数据放在一行而不是一列中,因为我有足够的列几乎已经可以跨越屏幕了。
请注意,as("", class(x))
非常不可靠(例如,不适用于 Date
class)。可能有必要强制整个 table 先串起来。
要将多列打印为一列,请使用 sprintf
:
print(x=dt[,{
mx = max(Score)
subdat = .(
Description = as.character(Description),
Type = as.character(Type)
)
.(mx, sprintf("%10s %15s", subdat$Type, subdat$Description))
}, by=Batch], nrows=Inf, row.names=FALSE)
Batch V1 V2
A 3 fruit apple
A 3 beverage pepsi
A 3 food rice
B 2 beverage coke
B 2 fruit banana
C 1 food butter
D 1 food bread
这是非常手动的,但从 sapply(dt, function(x) max(nchar(x)))
.