添加一个新的数据框列,该列中有一个list1中的值,将数据框中的列值与另一个列表list2中存在的值匹配。 list1和list2的长度相同
我可以使用for循环来做到这一点,但是我想要一种有效的方法来做到这一点。任何建议都会有所帮助。
DF1 <- data.frame(c1=rep(1:3, 2), c2=rep(c(0.1, 0.2, 0.3), 2))
list1 <- c(1, 2, 3)
list2 <- c(0.6, 0.7, 0.8)
这是输入
DF1
c1 c2
1 1 0.1
2 2 0.2
3 3 0.3
4 1 0.1
5 2 0.2
6 3 0.3
这是我所期望的 应该创建一个新列c3(具有list2中的值),以将column(c1)中的值与list1中的值相匹配
DF1
c1 c2 c3
1 1 0.1 0.6
2 2 0.2 0.7
3 3 0.3 0.8
4 1 0.1 0.6
5 2 0.2 0.7
6 3 0.3 0.8
答案 0 :(得分:2)
您可以制作一个数据框进行查找,例如
DF2 <- data.frame(c1 = 1:3, c3 = c(0.6, 0.7, 0.8))
,然后加入merge
或dplyr::left_join
。例如:
merge(DF1, DF2, by.x = "c1", by.y = "c1")
或
left_join(DF1, DF2, by = "c1")
答案 1 :(得分:1)
您可以使用匹配项,即
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.android.kotlincoroutines"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
androidExtensions {
experimental = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// ViewModel and LiveData
def lifecycle_version = '2.0.0-beta01'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-alpha02"
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
// Room for database
def room_version = '2.0.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
def work_version = "1.0.0-rc01"
implementation "android.arch.work:work-runtime:$work_version"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation "com.google.truth:truth:0.42"
androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0-M1'
}