簡體   English   中英

從R中的data.frame中選擇嵌套字段和非嵌套字段

[英]select both nested and not nested fields from a data.frame in R

是否可以從data.frame中選擇嵌套字段和不嵌套字段?

例如:

df=data.frame(
     a=c(1,2,3)
    ,b=c(4,5,6)
)
nested=data.frame(
     a=c(10,20,30)
    ,b=c(40,50,60)
)
df$nested=nested

print(df)
#   a b nested.a nested.b
#   1 4       10       40
#   2 5       20       50
#   3 6       30       60

我知道我可以這樣選擇不嵌套的字段

df[,c("a","b")] 
#   a b
#   1 4
#   2 5
#   3 6

並以其他方式嵌套字段

df$nested[,c("a","b")] 
#    a  b
#   10 40
#   20 50
#   30 60

我想做這樣的事情:

df_new=df[,c("a","nested$a")]
#    a  nested.a
#    1 10
#    2 20
#    3 30

我們可以unnest通過轉換為它data.framedo.call ,然后選擇下面的列名

do.call(data.frame, df)[c('a', 'nested.a')]
#   a nested.a
#1 1       10
#2 2       20
#3 3       30

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM