拆分因子级别

时间:2018-05-23 19:26:36

标签: r dataframe

这是一个看似简单的问题,我无法找到答案。

我有一个数据框

df <- data.frame(respondent = factor(c(1, 2, 3, 4, 5, 6)), language = factor(c("English", "English", "French", "French, German", "German", "German")))

因子级别名称反映了调查回复。大多数受访者都是单语,但有些人会说法语和德语。我想分开&#34;法语,德语&#34;等级为两个。

如何做到这一点?

1 个答案:

答案 0 :(得分:2)

我们可以使用let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "your-shared-secret") SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in switch result { case .success(let receipt): let productId = "com.musevisions.SwiftyStoreKit.Subscription" // Verify the purchase of a Subscription let purchaseResult = SwiftyStoreKit.verifySubscription( ofType: .autoRenewable, // or .nonRenewing (see below) productId: productId, inReceipt: receipt) switch purchaseResult { case .purchased(let expiryDate, let items): print("\(productId) is valid until \(expiryDate)\n\(items)\n") case .expired(let expiryDate, let items): print("\(productId) is expired since \(expiryDate)\n\(items)\n") case .notPurchased: print("The user has never purchased \(productId)") } case .error(let error): print("Receipt verification failed: \(error)") } } 中的separate_rows,然后使用tidyrmutate转换回language。生成的factor列将是每个单独语言的级别因素:

language

<强>结果:

library(dplyr)
library(tidyr)

df = df %>%
  separate_rows(language) %>%
  mutate(language = factor(language))