Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将列表列强制转换为R数据帧中的字符串_R - Fatal编程技术网

将列表列强制转换为R数据帧中的字符串

将列表列强制转换为R数据帧中的字符串,r,R,创建示例数据: id <- c(12, 32, 42, 42, 52, 52, 67, 67) relationship_id <- c(15,1,59,1,61,6,59,1) sample.data <- data.frame(id,relationship_id) 但当我尝试将列表向量强制为字符向量时: combo["relationship_id"] <- lapply(combo["relationship_id"], as.character) > h

创建示例数据:

id <- c(12, 32, 42, 42, 52, 52, 67, 67)
relationship_id <- c(15,1,59,1,61,6,59,1)
sample.data <- data.frame(id,relationship_id)
但当我尝试将列表向量强制为字符向量时:

combo["relationship_id"] <- lapply(combo["relationship_id"], as.character)
> head(combo)    
  id relationship_id
1 12              15
2 32               1
3 42    c("59", "1")
4 52    c("61", "6")
5 67    c("59", "1")
combo[“关系id”]头(combo)
id关系\u id
1 12              15
2 32               1
342 c(“59”、“1”)
452C(“61”、“6”)
567C(“59”、“1”)

它包括连接语法。。。我知道我可以解析输出,使其可用,但为什么会发生这种情况?是否有更简单的方法来清理输出?

您试图解决错误的问题。如果您确实想将这些值折叠成单个字符向量,则应使用
collapse=“,”
而不是
sep

combo <- aggregate(relationship_id ~ id, data = sample.data, 
                   paste, collapse=",")
table(combo$relationship_id)
# 
#    1   15 59,1 61,6 
#    1    1    2    1 

combo将
sep
更改为
collapse
,您应该能够做您期望的事情。将一个可重复的问题作为您的第一个问题做得很好。(+1)对于这种情况,我喜欢
toString
。我发现
paste
更透明,使用更广泛,但我想这是一个偏好问题。
combo["relationship_id"] <- lapply(combo["relationship_id"], as.character)
> head(combo)    
  id relationship_id
1 12              15
2 32               1
3 42    c("59", "1")
4 52    c("61", "6")
5 67    c("59", "1")
combo <- aggregate(relationship_id ~ id, data = sample.data, 
                   paste, collapse=",")
table(combo$relationship_id)
# 
#    1   15 59,1 61,6 
#    1    1    2    1