我正在r中做一个项目,正在分析文本。我有
形式的字符串 "id": 8784, "name": "Daniel", "age":"65", "gender":"M"
和这类事情。我的问题是如何获取数据,以便每个变量都可以成为自己的列,即名称列,第一个条目是Daniel。
答案 0 :(得分:2)
最简单的方法是使用sealed trait ILevel
case object FooLevel extends ILevel
case object CompanionLevel extends ILevel
abstract class Foo[T,I](val msg: I) {
type Level <: ILevel
}
object Foo {
implicit def provide[T]: Foo[T,String] =
new Foo[T,String]("I came from a place you can't touch so subtyping can't help you") {
override type Level = FooLevel.type
}
}
class User
object User {
implicit object userFoo extends Foo[User,Int](42) {
type Level = CompanionLevel.type
}
}
type CompanionLevelFoo[T,I] = Foo[T,I] {type Level = CompanionLevel.type }
def fooOf[T,I](U: T)(implicit tc2: CompanionLevelFoo[T,I]): Foo[T, I] = tc2
fooOf(new User).msg
路径
JSON
如果有多个字符串,我们可以使用
library(jsonlite)
library(tidyverse)
sprintf("{%s}", str1) %>%
fromJSON %>%
as.data.frame
# id name age gender
#1 8784 Daniel 65 M
根据OP的评论,如果它还有sprintf("{%s}", str1) %>%
map_df(fromJSON)
[{..}]
对于多个元素,
str1 <- '[{"id": 8784, "name": "Daniel", "age":"65", "gender":"M"}]'
fromJSON(str1)
# id name age gender
#1 8784 Daniel 65 M
str1 <- c(str1, str1)
str1 %>%
map_df(fromJSON)
# id name age gender
#1 8784 Daniel 65 M
#2 8784 Daniel 65 M