从其他列中取消列出数据框列保留信息 [英] Unlist data frame column preserving information from other column

查看:91
本文介绍了从其他列中取消列出数据框列保留信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,由两列组成:一个字符向量 col1 和一个列表 col2

  myVector<  -  c(A,B,C,D)

myList< - list()
myList [[1]]< - c(1,4,6,7)
myList [[2]]< - c ,7,3)
myList [[3]]< - c(5,5,3,9,6)
myList [[4]]< - c(7,9)

myDataFrame< - data.frame(row = c(1,2,3,4))

myDataFrame $ col1< - myVector
myDataFrame $ col2 < - myList

myDataFrame
#row col1 col2
#1 1 A 1,4,6,7
#2 2 B 2,7,3
#3 3 C 5,5,3,9,6
#4 4 D 7,9

我想取消列出我的 col2 仍然保留列表中每个元素的元素存储在 col1 。然后在一天结束时,我想要两个长度等于 length的向量(unlist(myDataFrame $ col2))。在代码中:

 #unlist myList 
unlist.col2< - unlist(myDataFrame $ col2)
unlist.col2
#[1] 1 4 6 7 2 7 3 5 5 3 9 6 7 9

#unlist myVector获取
#unlist.col1< - ? ??
#unlist.col1
#[1] AAAABBBCCCCCDD

我不能想到任何直接的方式来获得它。

解决方案

这里的想法是首先使用 sapply 然后使用 rep 复制 col1 长度

  l1<  -  sapply(myDataFrame $ col2,length)
unlist.col1< rep(myDataFrame $ col1,l1)
unlist.col1
#[1]AAAABBBCC CCCDD

或者由@Ananda Mahto,以上也可以使用($ data code)vapply

  ,AAAABBBCC的代表(col1,vapply(col2,length,1L)))
# CCCDD


I have a data frame which consists of two column: a character vector col1 and a list col2.

myVector <- c("A","B","C","D")

myList <- list()
myList[[1]] <- c(1, 4, 6, 7)
myList[[2]] <- c(2, 7, 3)
myList[[3]] <- c(5, 5, 3, 9, 6)
myList[[4]] <- c(7, 9)

myDataFrame <- data.frame(row = c(1,2,3,4))

myDataFrame$col1 <- myVector
myDataFrame$col2 <- myList

myDataFrame
# row col1          col2
# 1   1    A    1, 4, 6, 7
# 2   2    B       2, 7, 3
# 3   3    C 5, 5, 3, 9, 6
# 4   4    D          7, 9

I want to unlist my col2 still keeping for each element of the vectors in the list the information stored in col1. Then at the end of the day I want two vectors of length equal to length(unlist(myDataFrame$col2)). In code:

# unlist myList
unlist.col2 <- unlist(myDataFrame$col2)
unlist.col2
# [1] 1 4 6 7 2 7 3 5 5 3 9 6 7 9

# unlist myVector to obtain
# unlist.col1 <- ???
# unlist.col1
# [1] A A A A B B B C C C C C D D

I can't think of any straightforward way to get it.

解决方案

Here, the idea is to first get the length of each list element using sapply and then use rep to replicate the col1 with that length

 l1 <- sapply(myDataFrame$col2, length)
  unlist.col1 <- rep(myDataFrame$col1, l1)
  unlist.col1
 #[1] "A" "A" "A" "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D"

Or as suggested by @Ananda Mahto, the above could be also done with vapply

   with(myDataFrame, rep(col1, vapply(col2, length, 1L)))
  #[1] "A" "A" "A" "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D"

这篇关于从其他列中取消列出数据框列保留信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆