我有一个<cfset myObj = createObject("java","javax.xml.parsers.DocumentBuilderFactory")>
<cfset createDocs = myObj.newInstance().newDocumentBuilder()>
<cfset parseDocs = createDocs.parse(expandpath('/testParse.xml'))>
<cfset getNodeName = parseDocs.getDocumentElement().getNodeName()>
<cfset getList = parseDocs.getElementsByTagName("staff")>
<cfloop index="i" from="1" to="#getList.getlength()#">
<!--- Do your business logic here --->
</cfloop>
,其中有几列:
data.frame
然后添加另外一列set.seed(1)
df <- data.frame(cluster=LETTERS[1:4],group=c(rep("m",2),rep("f",2)),point=rnorm(4),err=runif(4,0.1,0.3))
"\n"
其相应行的所有列,其中列名位于值之前。
我知道这一点:
concatenates
给我这个library(tidyr)
library(dplyr)
tidyr::unite(df,text,sep="\n")
:
tibble
但是我想要的是这个 text
1 A\nm\n0.487429052428485\n0.286941046221182
2 B\nm\n0.738324705129217\n0.142428504256532
3 C\nf\n0.575781351653492\n0.230334753217176
4 D\nf\n-0.305388387156356\n0.125111019192263
:
tibble
有什么主意吗?
答案 0 :(得分:3)
我们可以将Map
与do.call
一起使用
data.frame(text = do.call(paste, c(Map(function(x, y)
paste(x, y, sep=": "), names(df), df), sep="\n")))
或者使用tidyverse
,在各列之间进行映射(imap
-提供.y
作为列名),然后执行unite
library(tidyverse)
imap(df, ~ paste(.y, .x, sep=": ")) %>%
as_tibble %>%
unite(text, sep="\n")
# A tibble: 4 x 1
# text
# <chr>
#1 "cluster: A\ngroup: m\npoint: -0.626453810742332\nerr: 0.225822808779776"
#2 "cluster: B\ngroup: m\npoint: 0.183643324222082\nerr: 0.112357254093513"
#3 "cluster: C\ngroup: f\npoint: -0.835628612410047\nerr: 0.14119491497986"
#4 "cluster: D\ngroup: f\npoint: 1.59528080213779\nerr: 0.135311350505799"